Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changes
Unreleased
----------

- Fixed ``HistoricForeignKey`` behaviour when used together with ``prefetch_related()``
Copy link
Member

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 🙂

Suggested change
- Fixed ``HistoricForeignKey`` behaviour when used together with ``prefetch_related()``
- Fixed ``HistoricForeignKey``'s behaviour when used together with
``prefetch_related()``, which caused some related objects to be missing (gh-1152)


3.3.0 (2023-03-08)
------------------
Expand Down
2 changes: 1 addition & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ def get_queryset(self):
)
else:
queryset = super().get_queryset()
return self._apply_rel_filters(queryset)
return queryset

return create_reverse_many_to_one_manager(
HistoricRelationModelManager, self.rel
Expand Down
93 changes: 93 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The 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
def test_non_historic_to_historic_prefetch(self):
def test_non_historic_to_historic_prefetch(self):
"""Test that the field works properly with ``prefetch_related()`` when defined
on a model *without* history tracking, and with the ``to`` arg set to a model
*with* history tracking."""

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all() after prefetch_related() (or most other queryset-returning methods) is unnecessary.

Suggested change
).all()
)

The same goes for the similar calls to all() in the two test cases below.


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],
)