mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
[1.5.x] Fixed #18375 -- Removed dict-ordering dependency for F-expressions
F() expressions reuse joins like any lookup in a .filter() call -
reuse multijoins generated in the same .filter() call else generate
new joins. Also, lookups can now reuse joins generated by F().
This change is backwards incompatible, but it is required to prevent
dict randomization from generating different queries depending on
.filter() kwarg ordering. The new way is also more consistent in how
joins are reused.
Backpatch of 90b86291d0
This commit is contained in:
@@ -219,3 +219,42 @@ class ExpressionsTests(TestCase):
|
||||
)
|
||||
acme.num_employees = F("num_employees") + 16
|
||||
self.assertRaises(TypeError, acme.save)
|
||||
|
||||
def test_ticket_18375_join_reuse(self):
|
||||
# Test that reverse multijoin F() references and the lookup target
|
||||
# the same join. Pre #18375 the F() join was generated first, and the
|
||||
# lookup couldn't reuse that join.
|
||||
qs = Employee.objects.filter(
|
||||
company_ceo_set__num_chairs=F('company_ceo_set__num_employees'))
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
||||
|
||||
def test_ticket_18375_kwarg_ordering(self):
|
||||
# The next query was dict-randomization dependent - if the "gte=1"
|
||||
# was seen first, then the F() will reuse the join generated by the
|
||||
# gte lookup, if F() was seen first, then it generated a join the
|
||||
# other lookups could not reuse.
|
||||
qs = Employee.objects.filter(
|
||||
company_ceo_set__num_chairs=F('company_ceo_set__num_employees'),
|
||||
company_ceo_set__num_chairs__gte=1)
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
||||
|
||||
def test_ticket_18375_kwarg_ordering_2(self):
|
||||
# Another similar case for F() than above. Now we have the same join
|
||||
# in two filter kwargs, one in the lhs lookup, one in F. Here pre
|
||||
# #18375 the amount of joins generated was random if dict
|
||||
# randomization was enabled, that is the generated query dependend
|
||||
# on which clause was seen first.
|
||||
qs = Employee.objects.filter(
|
||||
company_ceo_set__num_employees=F('pk'),
|
||||
pk=F('company_ceo_set__num_employees')
|
||||
)
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 1)
|
||||
|
||||
def test_ticket_18375_chained_filters(self):
|
||||
# Test that F() expressions do not reuse joins from previous filter.
|
||||
qs = Employee.objects.filter(
|
||||
company_ceo_set__num_employees=F('pk')
|
||||
).filter(
|
||||
company_ceo_set__num_employees=F('company_ceo_set__num_employees')
|
||||
)
|
||||
self.assertEqual(str(qs.query).count('JOIN'), 2)
|
||||
|
||||
Reference in New Issue
Block a user