Fix fetching related object with ID = 0 #1366
Merged
+13
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, when trying to fetch a related object that has an integer primary key that equals to 0, the foreign key is set to
Noneinstead and the related object is not fetched.Description
Although it's uncommon, it's possible to have a row with integer primary key equal to 0 or even negative.
An application I am working at has a table with such a row, and another table relates to it,
prefetch_relatedandfetch_relateddoesn't work correctly. Instead of fetching the related object, the foreign key field is set toNone.This pull request tries to fix that.
Motivation and Context
This pull request attempts to fix the issue described above.
How Has This Been Tested?
I've added a test in test_relations.py called
test_fetch_fk_zero. It creates two Tournament objects with hard-coded IDs of 0 and 1, and then creates an Event for both of them. The test fails before my change, because the Tournament with ID=0 is not fetched when trying to use bothfetch_relatedandprefetch_relatedon an Event.The change doesn't seem to affect other tests, but because this is my first attempt to contribute to this project, it would be good for someone else to verify.
Checklist: