- changed status to resolved
when "distinct" is used with ORM query, add all ORDER BY columns to the colums clause of the select
Issue #786
resolved
postgres requires columns to be in the columns clause when used with distinct/order by:
sqlalchemy.exceptions.SQLError: (ProgrammingError) for SELECT DISTINCT, ORDER BY
expressions must appear in select list
'SELECT DISTINCT promo_promotion.mechanic_id AS promo_promotion_mechanic_id, pr
omo_promotion.id AS promo_promotion_id, promo_promotion.name AS promo_promotion_
name \nFROM promo_promotion JOIN prod_product ON promo_promotion.id = prod_produ
ct.promotion_id JOIN promo_mechanic ON promo_mechanic.id = promo_promotion.mecha
nic_id \nWHERE lower(prod_product.name) LIKE %(lower)s ORDER BY promo_mechanic.n
ame ASC' {'lower': '%1%'}
so, add the ORDER BY columns to the columns clause, like an add_column()
, but not actually returning the column. will require not adding the same column twice if the col was explicitly added via add_column().
Comments (2)
-
reporter -
reporter - removed milestone
Removing milestone: 0.4.xx (automated comment)
- Log in to comment
yeah, we had this feature and it broke at some point but its been fixed in the last few dozen revisions. aaaac9be475c091f0a1dcb46b76168e6920aa863 adds explicit test cases.