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

[SQL] Using null values in em.find's FilterQuery leads to unexpected results #548

Closed
darkbasic opened this issue May 6, 2020 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@darkbasic
Copy link
Collaborator

darkbasic commented May 6, 2020

@Entity()
export class Message extends BaseIdEntity {
  @ManyToMany({pivotTable: 'message_to_deletion'})
  deletions: Collection<User> = new Collection<User>(this);

This is a query which matches a non-null value on the M:N deletions field:

em.find(Message, {
  group: groupId,
  deletions: 1,
});

This is the sql that gets generated:

select "e0".*, "e1"."message_id", "e1"."user_id"
from "message" as "e0"
left join "message_to_deletion" as "e1"
on "e0"."id" = "e1"."message_id"
where "e0"."group_id" = 1 and "e1"."user_id" = 1

So far so good.

Now let's try to match a null value instead:

em.find(Message, {
  group: groupId,
  deletions: null,
});

This is what gets generated:

select "e0".*, "e1"."message_id", "e1"."user_id"
from "message" as "e0"
left join "message_to_deletion" as "e1"
on "e0"."id" = "e1"."message_id"
where "e0"."group_id" = 25 and "e0"."deletions" is null

This obviously won't work, because deletions is not a column.

Expected behavior

select "e0".*, "e1"."message_id", "e1"."user_id"
from "message" as "e0"
left join "message_to_deletion" as "e1"
on "e0"."id" = "e1"."message_id"
where "e0"."group_id" = 1 and "e1"."user_id" is null

Additional context

em.find(Message, {
  group: groupId,
  deletions: {id: null},
});

The previous workaround works, but with some caveats: in fact having to explicitly specify the PK column will trigger an unnecessary join, see #549

Versions

Dependency Version
node v13.14.0
typescript 3.8.3
mikro-orm 3.6.11
pg 8.0.3
@B4nan
Copy link
Member

B4nan commented Jun 7, 2020

Closing as fixed in dev via fdc9e06

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