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

LoadStrategy not honored on ManyToOne #1657

Closed
evantrimboli opened this issue Apr 7, 2021 · 2 comments
Closed

LoadStrategy not honored on ManyToOne #1657

evantrimboli opened this issue Apr 7, 2021 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@evantrimboli
Copy link
Contributor

evantrimboli commented Apr 7, 2021

Describe the bug
If I specify the strategy on a ManyToOne to be joined, the query doesn't do a join to load.

Stack trace
N/A

To Reproduce
Steps to reproduce the behavior:

Consider the following entities:

@Entity()
export default class Order {
  @PrimaryKey()
  id!: number;

  @OneToMany(() => OrderItem, ({ order }) => order)
  orderItems = new Collection<OrderItem>(this);
}

@Entity()
export default class OrderItem {
  @PrimaryKey()
  id!: number;

  @ManyToOne({
    entity: () => Order,
    strategy: LoadStrategy.JOINED,
    eager: true
  })
  order!: Order;
}

Finally, run a query to pull order items:

orm.em.getRepository<OrderItem>('OrderItem').find({
  id: {
    $gt: 100
  }
})

The generated query is:

select "e0".* from "order_item" as "e0" where "e0"."id" > 100

Expected behavior
There should be a join in the query to populate the Order entity.

Additional context
Specifying populate directly in the query does produce the join:

orm.em
  .getRepository<OrderItem>('OrderItem')
  .find({ id: { $gt: 100 } }, { populate: { order: LoadStrategy.JOINED } });
select "e0"."id", "e0"."order_id", "o1"."id" as "o1__id" from "order_item" as "e0" left join "order" as "o1" on "e0"."order_id" = "o1"."id" where "e0"."id" > 100

Versions

Dependency Version
node 14.15.4
typescript 4.1.5
mikro-orm 4.5.0
your-driver postgresql - 4.5.0
@B4nan
Copy link
Member

B4nan commented Apr 8, 2021

I think this is more about combining strategy with eager: true (eager loading is implemented as automatically filling the populate for you).

@B4nan B4nan added the bug Something isn't working label Apr 8, 2021
@evantrimboli
Copy link
Contributor Author

evantrimboli commented Apr 9, 2021

Just FYI, I tried it with only eager: true and omitted the load strategy and I had the same result, no join.

@ManyToOne({
  entity: () => Order,
  eager: true
})

@B4nan B4nan closed this as completed in 897c7bd Apr 9, 2021
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

2 participants