-
-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix HistoricForeignKey when used together with prefetch_related() #1159
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2545,3 +2545,96 @@ def test_historic_to_historic(self): | |||||||||||
)[0] | ||||||||||||
pt1i = pt1h.instance | ||||||||||||
self.assertEqual(pt1i.organization.name, "original") | ||||||||||||
|
||||||||||||
def test_non_historic_to_historic_prefetch(self): | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a docstring detailing a little more explicitly what the test case is testing 🙂 E.g. something like this: (feel free to adjust the text)
Suggested change
The same goes for the two test cases below. |
||||||||||||
org1 = TestOrganizationWithHistory.objects.create(name="org1") | ||||||||||||
org2 = TestOrganizationWithHistory.objects.create(name="org2") | ||||||||||||
|
||||||||||||
p1 = TestParticipantToHistoricOrganization.objects.create( | ||||||||||||
name="p1", organization=org1 | ||||||||||||
) | ||||||||||||
p2 = TestParticipantToHistoricOrganization.objects.create( | ||||||||||||
name="p2", organization=org1 | ||||||||||||
) | ||||||||||||
p3 = TestParticipantToHistoricOrganization.objects.create( | ||||||||||||
name="p3", organization=org2 | ||||||||||||
) | ||||||||||||
p4 = TestParticipantToHistoricOrganization.objects.create( | ||||||||||||
name="p4", organization=org2 | ||||||||||||
) | ||||||||||||
|
||||||||||||
with self.assertNumQueries(2): | ||||||||||||
record1, record2 = TestOrganizationWithHistory.objects.prefetch_related( | ||||||||||||
"participants" | ||||||||||||
).all() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The same goes for the similar calls to |
||||||||||||
|
||||||||||||
self.assertListEqual( | ||||||||||||
[p.name for p in record1.participants.all()], | ||||||||||||
[p1.name, p2.name], | ||||||||||||
) | ||||||||||||
self.assertListEqual( | ||||||||||||
[p.name for p in record2.participants.all()], | ||||||||||||
[p3.name, p4.name], | ||||||||||||
) | ||||||||||||
|
||||||||||||
def test_historic_to_non_historic_prefetch(self): | ||||||||||||
org1 = TestOrganization.objects.create(name="org1") | ||||||||||||
org2 = TestOrganization.objects.create(name="org2") | ||||||||||||
|
||||||||||||
p1 = TestHistoricParticipantToOrganization.objects.create( | ||||||||||||
name="p1", organization=org1 | ||||||||||||
) | ||||||||||||
p2 = TestHistoricParticipantToOrganization.objects.create( | ||||||||||||
name="p2", organization=org1 | ||||||||||||
) | ||||||||||||
p3 = TestHistoricParticipantToOrganization.objects.create( | ||||||||||||
name="p3", organization=org2 | ||||||||||||
) | ||||||||||||
p4 = TestHistoricParticipantToOrganization.objects.create( | ||||||||||||
name="p4", organization=org2 | ||||||||||||
) | ||||||||||||
|
||||||||||||
with self.assertNumQueries(2): | ||||||||||||
record1, record2 = TestOrganization.objects.prefetch_related( | ||||||||||||
"participants" | ||||||||||||
).all() | ||||||||||||
|
||||||||||||
self.assertListEqual( | ||||||||||||
[p.name for p in record1.participants.all()], | ||||||||||||
[p1.name, p2.name], | ||||||||||||
) | ||||||||||||
self.assertListEqual( | ||||||||||||
[p.name for p in record2.participants.all()], | ||||||||||||
[p3.name, p4.name], | ||||||||||||
) | ||||||||||||
|
||||||||||||
def test_historic_to_historic_prefetch(self): | ||||||||||||
org1 = TestOrganizationWithHistory.objects.create(name="org1") | ||||||||||||
org2 = TestOrganizationWithHistory.objects.create(name="org2") | ||||||||||||
|
||||||||||||
p1 = TestHistoricParticipanToHistoricOrganization.objects.create( | ||||||||||||
name="p1", organization=org1 | ||||||||||||
) | ||||||||||||
p2 = TestHistoricParticipanToHistoricOrganization.objects.create( | ||||||||||||
name="p2", organization=org1 | ||||||||||||
) | ||||||||||||
p3 = TestHistoricParticipanToHistoricOrganization.objects.create( | ||||||||||||
name="p3", organization=org2 | ||||||||||||
) | ||||||||||||
p4 = TestHistoricParticipanToHistoricOrganization.objects.create( | ||||||||||||
name="p4", organization=org2 | ||||||||||||
) | ||||||||||||
|
||||||||||||
with self.assertNumQueries(2): | ||||||||||||
record1, record2 = TestOrganizationWithHistory.objects.prefetch_related( | ||||||||||||
"historic_participants" | ||||||||||||
).all() | ||||||||||||
|
||||||||||||
self.assertListEqual( | ||||||||||||
[p.name for p in record1.historic_participants.all()], | ||||||||||||
[p1.name, p2.name], | ||||||||||||
) | ||||||||||||
self.assertListEqual( | ||||||||||||
[p.name for p in record2.historic_participants.all()], | ||||||||||||
[p3.name, p4.name], | ||||||||||||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful being a little more explicit on what the bug used to be; also, adding the issue number 🙂