Composite primary key subquery lookup prevent usage or specify fields and are not implemented for exact
I started playing with tuple lookup support for composite primary key and I noticed two main problems.
First pk__in=query
lookups completely disallow specifying which fields should be used in the select clause of the right-hand-side and implicitly set them to the left-hand-side name which assumes the same set of left-hand-side field name is shared by the right-hand-side which is a bad assumption
-
diff --git a/tests/composite_pk/models/tenant.py b/tests/composite_pk/models/tenant.py
index 6286ed2354..c85869afa7 100644
a
|
b
|
class Comment(models.Model):
|
44 | 44 | related_name="comments", |
45 | 45 | ) |
46 | 46 | text = models.TextField(default="", blank=True) |
| 47 | integer = models.IntegerField(default=0) |
47 | 48 | |
48 | 49 | |
49 | 50 | class Post(models.Model): |
-
diff --git a/tests/composite_pk/test_filter.py b/tests/composite_pk/test_filter.py
index fe942b9e5b..78383655a0 100644
a
|
b
|
def test_filter_comments_by_pk_in(self):
|
182 | 182 | Comment.objects.filter(pk__in=pks).order_by("pk"), objs |
183 | 183 | ) |
184 | 184 | |
| 185 | def test_filter_comments_by_pk_in_subquery(self): |
| 186 | self.assertSequenceEqual( |
| 187 | Comment.objects.filter( |
| 188 | pk__in=Comment.objects.filter(pk=self.comment_1.pk), |
| 189 | ), |
| 190 | [self.comment_1], |
| 191 | ) |
| 192 | self.assertSequenceEqual( |
| 193 | Comment.objects.filter( |
| 194 | pk__in=Comment.objects.filter(pk=self.comment_1.pk).values( |
| 195 | "tenant_id", "id" |
| 196 | ), |
| 197 | ), |
| 198 | [self.comment_1], |
| 199 | ) |
| 200 | self.comment_2.integer = self.comment_1.id |
| 201 | self.comment_2.save() |
| 202 | self.assertSequenceEqual( |
| 203 | Comment.objects.filter( |
| 204 | pk__in=Comment.objects.values("tenant_id", "integer"), |
| 205 | ), |
| 206 | [self.comment_1], |
| 207 | ) |
| 208 | |
185 | 209 | def test_filter_comments_by_user_and_order_by_pk_asc(self): |
186 | 210 | self.assertSequenceEqual( |
187 | 211 | Comment.objects.filter(user=self.user_1).order_by("pk"), |
Allowing fields to be specified requires lifting a QuerySet
resolving time constraint which was entirely by-passable by passing queryset.query
instead as the right-hand-side.
The tuple exact lookup should also allow querysets with a single element to be specified as right-hand-side which is currently disallowed
Lastly lifting the constraint that prevented subqueries returning more than one column from being used as a right-hand-side required adjusting the existing exact
and in
lookup logic to disallow left-hand-side and right-hand-side with mismatching number of fields.
Change History
(7)
Cc: |
Csirmaz Bendegúz added
|
Has patch: |
unset
|
Owner: |
set to Simon Charette
|
Triage Stage: |
Unreviewed → Accepted
|
Patch needs improvement: |
set
|
Patch needs improvement: |
unset
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
Thank you!