Opened 14 years ago
Closed 12 years ago
#15146 closed Bug (duplicate)
Reverse relations for unsaved objects include objects with NULL foreign key
Reported by: | treyh | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Model with parent relation and children reverse relation:
class TestModel(models.Model): parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
Bug found while using model:
t = TestModel() t.save() u = TestModel() u.children.all() # Incorrectly returns: [<TestModel: TestModel object>] u.save() u.children.all() # Correctly returns: []
Attachments (3)
Change History (14)
comment:1 by , 14 years ago
Summary: | Reverse relations include unsaved objects (NULL id) → Reverse relations include objects with NULL foreign key |
---|
comment:2 by , 14 years ago
Summary: | Reverse relations include objects with NULL foreign key → Reverse relations for unsaved objects include objects with NULL foreign key |
---|
comment:3 by , 14 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 14 years ago
Owner: | changed from | to
---|
by , 14 years ago
Attachment: | patch.diff added |
---|
comment:5 by , 14 years ago
Has patch: | set |
---|
comment:6 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:7 by , 14 years ago
Patch needs improvement: | set |
---|
Could you combine the fix and tests into one single diff patch?
comment:8 by , 14 years ago
Easy pickings: | unset |
---|---|
Owner: | changed from | to
comment:9 by , 13 years ago
UI/UX: | unset |
---|
Ticket #17541 is a duplicate. I don't know which one to close at this point.
comment:10 by , 12 years ago
As I said on the other ticket, please upgrade the severity of this. It has just caused entirely sane-looking code to delete over half our database rows. This needs to be fixed ASAP, the patch has been submitted over two years ago.
comment:11 by , 12 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Closing this one as a duplicate of #17541. It doesn't really matter which one we leave open, as long as there's only one ticket per problem.
This is an edge case that exists when an object has been created, but not saved; at that point, the primary key of the object is NULL, so the reverse query does a lookup with that value.
The fix here will be to prevent related object lookups when pk=None.