Opened 5 years ago
Closed 5 years ago
#30639 closed Bug (duplicate)
Raise an error on unsupported operations following QuerySet.union().
Reported by: | Daniel Himmelstein | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | union, filter, warning |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
There is a major gotcha where several operations (such as .filter
and .exclude
) do not have any effect if applied to a queryset that has previously included a .union
statement. See for example this [StackOverflow](https://stackoverflow.com/a/49261966/4651668).
Here is a code example:
queryset_union = queryset_a.union(queryset_b) queryset_union.filter(key=value)
The last command has no effect. Ideally, it should apply the requested filter. However, from the [docs comment](https://docs.djangoproject.com/en/2.2/ref/models/querysets/#union) the following:
In addition, only LIMIT, OFFSET, COUNT(*), ORDER BY, and specifying columns (i.e. slicing, count(), order_by(), and values()/values_list()) are allowed on the resulting QuerySet. Further, databases place restrictions on what operations are allowed in the combined queries. For example, most databases don’t allow LIMIT or OFFSET in the combined queries.
Therefore, without adding the functionality, it would make sense to trigger a warning or raise an exception, such that users are aware that their filter statement did not succeed. IMO this seems like a NotImplementedError
. However, a warning would be more backwards compatible.
Change History (1)
comment:1 by , 5 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Summary: | Raise warning or error when user attempts an unsupported operation on a UNION queryset → Raise an error on unsupported operations following QuerySet.union(). |
Type: | Uncategorized → Bug |
Version: | 2.2 → master |
Duplicate of #27995.