Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QueryBuilder left join the same table twice bug #2602

Closed
deemaagog opened this issue Jan 5, 2022 · 1 comment
Closed

QueryBuilder left join the same table twice bug #2602

deemaagog opened this issue Jan 5, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@deemaagog
Copy link
Contributor

deemaagog commented Jan 5, 2022

Hi! thanks a bunch for this awesome ORM!
Describe the bug
I'm trying to join the same table twice (this is a simplified example)

const qb = await this.patternRepository
      .createQueryBuilder('patterns')
      .select(['patterns.id', 'user.id', 'user2.id'])
      .leftJoin('patterns.user', 'user')
      .leftJoin('patterns.user', 'user2')
      .getResultList();

and getting an error
invalid reference to FROM-clause entry for table "user"

this is the resulted sql query, seems like one left join is missing

select "patterns"."id", "user"."id", "user2"."id" from "pattern" as "patterns" left join "user" as "user2" on "patterns"."userId" = "user2"."id"

Expected behavior
No error

Versions

Dependency Version
node 14
typescript 4.1.3
mikro-orm 5
your-driver postgresql
@B4nan B4nan added the bug Something isn't working label Jan 5, 2022
@B4nan B4nan closed this as completed in b62fb05 Jan 5, 2022
@vincentwinkel
Copy link

vincentwinkel commented Oct 17, 2022

I don't know if it's relted, but I get an issue because of a table joined twice.
Basically, I have this structure (pseudo language, simplified for the example):

entity SeekerProfile {
  // ...
  mobilities: Collection(SeekerProfileMobility)
}

entity SeekerProfileMobility {
  // ...
  address: SeekerProfileAddress
  radius: int
}

entity SeekerProfileAddress {
  // ...
  lat: float
  lng: float
}

I would like to find all seeker profiles matching the location (the given coords should be inside their radius).
I'm trying something like:

// wrapper to .find({ filters }, [populate]) 
await this.seekerProfileService.load(
  {
    mobilities: {
      [expr(
        `(point(lng, lat) <@> point(${coords.lng}, ${coords.lat})) * 1.609344`,
      )]: {
        $gte: em.raw('seeker_profile_mobility.radius_internal'), // error here
      },
    },
  },
['mobilities.address'], // populate
);

This is the error I get, after running the SQL generated. How can I target my table seeker_profile_mobility without hard-code its generated alias?

Screenshot 2022-10-17 at 19 41 19

[EDIT]
I finally switched to using the query builder. I hope it will also improve the perfs.
The working solution looks like:

const distance = `(point(lng, lat) <@> point(${coords.lng}, ${coords.lat}))`;
const query = this.getRepo()
  .createQueryBuilder('t')
  .select('*')
  .leftJoin('t.mobilities', 'm')
  .leftJoin('m.address', 'a')
  .addSelect(`${distance} * 1.609344 AS distance`)
  .where(`${distance} <= m.radius_internal / 1.609344`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants