Opened 2 hours ago
Last modified 73 minutes ago
#37273 assigned Bug
Composite primary key exact lookups don’t support F() expressions on the RHS
| Reported by: | Pravin | Owned by: | Pravin |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | CompositeField, CompositePrimaryKey, Filter, RHS |
| Cc: | Pravin | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
An exact lookup on a CompositePrimaryKey accepts a literal tuple but rejects an F() expression referencing a compatible composite primary key.
for example,
class Comment(models.Model):
pk = models.CompositePrimaryKey("tenant_id", "id")
tenant_id = models.IntegerField()
id = models.IntegerField()
text = models.TextField()
The following works.
comment = Comment.objects.first() Comment.objects.filter(pk=(comment.tenant_id, comment.id))
but below one fails.
Comment.objects.filter(pk=models.F("pk"))
it raises
ValueError: 'exact' subquery lookup of 'pk' only supports OuterRef and QuerySet objects (received 'ColPairs')
Change History (4)
comment:1 by , 2 hours ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:2 by , 102 minutes ago
Thanks Sarah. Do i need to update release doc for it ? if yes then which version ?
Version 0, edited 102 minutes ago by (next)
comment:4 by , 73 minutes ago
| Has patch: | set |
|---|
Note:
See TracTickets
for help on using tickets.
Thank you for the report! Replicated
This is potentially a quick fix but I will let you investigate further
django/db/models/fields/tuple_lookups.py
ResolvedOuterRef, Query)):tests/composite_pk/test_filter.py