queryset.exists() returns False when queryset is distinct and sliced
|Reported by:||bitrut||Owned by:||err|
|Component:||Database layer (models, ORM)||Version:||1.4|
|Cc:||paluho@…, bitrut@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
SELECT (1) AS "a" FROM "user" LIMIT 1 OFFSET 1
so the result is correct.
But when you add distinct():
the SQL is
SELECT DISTINCT (1) AS "a" FROM "user" LIMIT 1 OFFSET 1
which returns nothing, so the exists() returns False which is incorrect.
DISTINCT narrows results to just one 1 and OFFSET omits the first result and goes to the next one which does not exist.
It's because DISTINCT has the higher priority than OFFSET in SQL.
I don't know the perfect solution. Maybe in the case of using distinct(), slicing and exists() together an exception should be thrown. Or the exists() function should notice this case and call count(), but this would lead to unexpected DB usage.
Change History (9)
comment:1 Changed 4 years ago by paluho@…
- Cc paluho@… added
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:4 Changed 4 years ago by err
- Owner changed from nobody to err
- Status changed from new to assigned