#25298: Forbid QuerySet filtering by a related field that expects a single value
when multiple values are returned
-------------------------------------+-------------------------------------
Reporter: timgraham | Owner: Y3K
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Y3K):
Replying to [comment:3 akaariai]:
> Notably
`Foo.objects.filter(related_model__exact=RelatedModel.objects.values('pk'))`
should work when the RelatedModel query returns just a single row. This is
a completely valid query, equivalent to:
> {{{
> SELECT ... FROM foo WHERE related_model_id = (SELECT id FROM
relatedmodel)
> }}}
> If the subquery returns one or zero rows, this query should work on all
backends.
Hello akaariai.
Don't you think it would be better to forbid such cases as well? If by any
means the subquery returns more than one row it'll fail. I think we can
save many user errors preventing this before it happens, if we raise an
exception whenever a subquery that may return multiple rows is used the
users will know and prevent said cases.
Open to comments and suggestions, of course.
--
Ticket URL: <https://code.djangoproject.com/ticket/25298#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.88e5594eb9f353d8c694867291120a9c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.