Open fluffy-critter opened 5 years ago
Okay, I found that if I added a .distinct()
after my .order_by()
that fixes it on my end. But .distinct()
doesn't appear as a method of a generated query in the docs; the closest I'm finding is https://docs.ponyorm.org/api_reference.html#distinct which makes it seem like it's only callable at the ORM package level.
It would also be helpful if https://docs.ponyorm.org/queries.html#automatic-distinct had a note e.g.
If for some reason the query isn't getting DISTINCT when you think it should be, add a
.distinct()
to it likequery = select(p for p in Person for c in p.cars if c.make in ("Toyota", "Honda")).distinct()
Short version
When I'm doing a complex JOIN-style query that spans multiple tables, the
SELECT DISTINCT
is not being applied when I think it should be.Long version
Hi,
I'm trying to add a tagging system to my blog engine. I build my queries using a cascade of queries where I apply multiple filters using a pattern like (pseudocodeish, you can see the actual code at https://github.com/PlaidWeb/Publ/blob/master/publ/queries.py):
and so on.
I'd like to be able to filter by more than one tag at a time, and so the actual filter I added was:
(where
tag_list
is, obviously, a list of tags). The relevant parts of my schema are:Anyway, the queries only ever involve selecting entries out, but when I try selecting from two tags I get duplicates for entries which have more than one tag in the selection list. Additionally, the generated query is:
So, the query is doing a select from both
Entry
andEntryTag
(rather than using aJOIN
as I'd expect), and is not doing aDISTINCT
as it should be.I will investigate using an explicit
left_join
query type, but in the meantime the documentation implies that this should work and get the automaticDISTINCT
query, since each of the individual queries is only selecting entries out.So, questions I have here:
SELECT DISTINCT
even if the query generator doesn't think it should?