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

Searching by referenced entity ID doesn't work #857

Closed
alpharder opened this issue Sep 22, 2020 · 1 comment
Closed

Searching by referenced entity ID doesn't work #857

alpharder opened this issue Sep 22, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@alpharder
Copy link

Hello there,

Account has many LedgerRecords
LedgerRecord has one Account

Prerequisites

@Entity({ tableName: 'accounts' })
export class Accounts {
  @PrimaryKey()
  id!: number;

  @OneToMany({ entity: () => LedgerRecords, mappedBy: lr => lr.account })
  ledgerRecords = new Collection<LedgerRecords>(this);
}
@Entity({ tableName: 'ledger_records' })
export class LedgerRecords {

  @PrimaryKey()
  id!: number;

  @ManyToOne({
    entity: () => Accounts,
    inversedBy: acc => acc.ledgerRecords,
    cascade: [Cascade.MERGE],
    fieldName: 'account_id',
  })
  account!: Accounts;
}

I did this type of call:

accountsRepo.findOne({
      ledgerRecords: {
        id: 18,
      }
});

Actual behavior:

select "e0".* from "accounts" as "e0" where "undefined"."id" = 18 limit 1 

Expected behavior

 select "e0".* from "accounts" as "e0" left join "ledger_records" as "e1" on "e0"."id" = "e1"."account_id" where "e1"."id" = 18 limit 1 

Additional details

Environment

node -v
v12.18.0
@mikro-orm/core @ 4.0.5
@mikro-orm/nestjs @ 4.0.0
@mikro-orm/postgresql @ 4.0.5
typescript @ 3.9.7

Workaround

accountsRepo.findOne({
      ledgerRecords: {
        $and: [
          { id: { $eq: 18 } },
          { id: { $ne: null } }
        ]
      }
    })

Thank you for the great library, have a good day!

@B4nan B4nan self-assigned this Sep 22, 2020
@B4nan B4nan added the bug Something isn't working label Sep 22, 2020
@B4nan B4nan closed this as completed in 920995f Sep 22, 2020
@alpharder
Copy link
Author

@B4nan thanks, you're awesome!

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