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

An $or-operator query with an $and-operator fails to construct properly. #247

Closed
jsprw opened this issue Nov 27, 2019 · 3 comments
Closed
Assignees
Labels
bug Something isn't working

Comments

@jsprw
Copy link
Contributor

jsprw commented Nov 27, 2019

Describe the bug
If you query like:

const items = [{ prop1: "value1", prop2: "value2" }, { prop1: "value3", prop2: "value4" }]; 
const connection = await Database.getConnection();
return connection.em.getRepository(Entity).find({
       $or: items.map(i => ({
           $and: [{ prop1: i.prop1, prop2: i.prop2}]
      }))
});

... it will construct a query like:

select `e0`.* from `entity` as `e0` where (((`e0`.`prop1` = 'value1' and `e0`.`prop2` = 'value2')) and ((`e0`.`prop1` = 'value3' and `e0`.`prop2` = 'value4')))

... instead of the expected:

select `e0`.* from `entity` as `e0` where (((`e0`.`prop1` = 'value1' and `e0`.`prop2` = 'value2')) or ((`e0`.`prop1` = 'value3' and `e0`.`prop2` = 'value4')))

Stack trace
No error, so no stack trace.

To Reproduce
Steps to reproduce the behavior:
See above.

Expected behavior
Create this query:

select `e0`.* from `entity` as `e0` where (((`e0`.`prop1` = 'value1' and `e0`.`prop2` = 'value2')) or ((`e0`.`prop1` = 'value3' and `e0`.`prop2` = 'value4')))

Additional context
It is related with the querybuilding using the incorrect synthax to append the group. I've created an additional repo where the query is constructed correctly, but I am not sure what effect the removal of that if has.

dev...jsprw:patch-2

Versions

Dependency Version
node v12.9.1
typescript v3.6.4
mikro-orm v3.0.0-rc
your-driver mysql2 v2.0.1
@jsprw jsprw added the bug Something isn't working label Nov 27, 2019
@B4nan
Copy link
Member

B4nan commented Nov 27, 2019

A bug indeed, this should be working as you expected, confirmed and replicated.

But you are overcomplicating it a bit, you can pass the items array directly and it will work as you want:

const items = [{ prop1: "value1", prop2: "value2" }, { prop1: "value3", prop2: "value4" }]; 
const connection = await Database.getConnection();
return connection.em.getRepository(Entity).find({
       $or: items
});

Multiple object keys are considered as ANDs, so prop1: { $and: [1,2] } is the same as prop1: [1,2].

@jsprw
Copy link
Contributor Author

jsprw commented Nov 27, 2019

Multiple object keys are considered as ANDs, so prop1: { $and: [1,2] } is the same as prop1: [1,2].

Thank you! It will make the code easier to read!

@B4nan B4nan closed this as completed in b9c00bc Dec 1, 2019
@B4nan
Copy link
Member

B4nan commented Dec 1, 2019

Fixed in 3.0.0-rc.1

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