According to the docs:
When present, the Postgresql dialect will render a DISTINCT ON
(>) construct.
So, passing column expressions to distinct()
works for PostgreSQL only (because there is DISTINCT ON
).
In the expression session.query(Tag).distinct(Tag.name).count()
sqlalchemy ignores Tag.name
and produces the query (distinct on all fields):
SELECT DISTINCT tag.country_id AS tag_country_id, tag.name AS tag_name
FROM tag
As you said, in your case distinct(Tag.name)
is applied - so instead of just count()
consider using this:
session.query(Tag).distinct(Tag.name).group_by(Tag.name).count()
Hope that helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…