by-crate-id query bug
jonathanstrong opened this issue · comments
https://github.com/mcorbin/meuse/blob/master/src/meuse/db/queries/category.clj#L27-L37
this code seems to return all categories, not just the categories associated with the crate.
I think the query needs to have the crate_id
condition as part of the where clause, not as part of the left-join.
exapmle:
meuse=# select c.id, c.description, c.name, cc.crate_id
from categories c
left join crates_categories cc on c.id=cc.category_id and cc.crate_id='2ff956f6-7b3e-49fc-8697-845d3a10fc75';
id | description | name | crate_id
--------------------------------------+--------------------------+--------------------------+--------------------------------------
5527b5af-84ec-432f-b4a5-698048e5d096 | statistics | statistics | 2ff956f6-7b3e-49fc-8697-845d3a10fc75
3759e640-2ba1-49c0-9ca3-10babdb07629 | science | science |
"science" row should not be part of query results. if you change the second clause of the left-join to be in the where clause instead, it works without returning extra rows:
meuse=# select c.id, c.description, c.name, cc.crate_id
from categories c
left join crates_categories cc on c.id=cc.category_id
where cc.crate_id='2ff956f6-7b3e-49fc-8697-845d3a10fc75';
id | description | name | crate_id
--------------------------------------+--------------------------+--------------------------+--------------------------------------
5527b5af-84ec-432f-b4a5-698048e5d096 | statistics | statistics | 2ff956f6-7b3e-49fc-8697-845d3a10fc75
fwiw I did try to fix this in the clojure code but failed :( - maybe next time.
figured it out, just needed to replace left-join
with join
. will file a PR shortly.
fixed version:
(defn by-crate-id
[crate-id]
(-> (h/select :c.id
:c.name
:c.description)
(h/from [:categories :c])
(h/join [:crates_categories :cc]
[:and
[:= :cc.category_id :c.id]
[:= :cc.crate_id crate-id]])
sql/format))