refresh_from_db fails to update null'ed ForeignKeys
I have User and Org models, with User having a ForeignKey to Org:
org = models.ForeignKey('Org', null=True, blank=True, on_delete=models.SET_NULL)
If I delete an Org instance then refresh the User instance, then user.org_id is cleared as expected, but user.org still references the deleted instance:
org = create_org()
user = create_user(org)
org.delete()
user.refresh_from_db()
self.assertIsNone(user.org_id) # Works!
self.assertIsNone(user.org) # Fails!
Change History
(7)
Component: |
Uncategorized → Database layer (models, ORM)
|
Severity: |
Normal → Release blocker
|
Triage Stage: |
Unreviewed → Accepted
|
Type: |
Uncategorized → Bug
|
Owner: |
changed from nobody to Anderson Resende
|
Status: |
new → assigned
|
Has patch: |
set
|
Owner: |
changed from Anderson Resende to Tim Graham
|
Triage Stage: |
Accepted → Ready for checkin
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
on_delete=models.SET_NULL
in theForeignKey
seems to be the important part of reproducing this.