when "distinct" is used with ORM query, add all ORDER BY columns to the colums clause of the select

Issue #786 resolved
Mike Bayer repo owner created an issue

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)

  1. Log in to comment