mongodb_ecto icon indicating copy to clipboard operation
mongodb_ecto copied to clipboard

Normalized query not putting parentheses around each where statement

Open diogo-ribeiro-bitmaker opened this issue 6 years ago • 0 comments
trafficstars

When using multiple piped where's and if these don't contain any "and" in them, the final query ignores the "and's" that should be between the where's and therefore its parentheses, leading to a wrong query that seems good. Eg.:

User
|> where([u], not is_nil(u.inserted_at) or u.inserted_at <= ^DateTime.utc_now())
|> where([u], is_nil(u.inserted_at) or u.inserted_at > ^DateTime.utc(now())

This query should return 0 results, but it will return all existing users. But if you add an and to a where, like the following, it will work well:

User
|> where([u], (not is_nil(u.inserted_at) or u.inserted_at <= ^DateTime.utc_now()) and not(is_nil(u.id)))
|> where([u], is_nil(u.inserted_at) or u.inserted_at > ^DateTime.utc(now())

diogo-ribeiro-bitmaker avatar May 13 '19 16:05 diogo-ribeiro-bitmaker