Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#25715 closed Bug (fixed)

refresh_from_db fails to update null'ed ForeignKeys

Reported by: Josh Kelley Owned by: Tim Graham
Component: Database layer (models, ORM) Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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)

comment:1 Changed 5 years ago by Tim Graham

Component: UncategorizedDatabase layer (models, ORM)
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

on_delete=models.SET_NULL in the ForeignKey seems to be the important part of reproducing this.

comment:2 Changed 5 years ago by Anderson Resende

Owner: changed from nobody to Anderson Resende
Status: newassigned

comment:3 Changed 5 years ago by Tim Graham

Has patch: set
Owner: changed from Anderson Resende to Tim Graham

@andersonresende, sorry to take this one if you made any progress on it, but I wanted to make sure it's fixed for the next 1.8.x release. Feel free to review my patch if you can.

PR

comment:4 Changed 5 years ago by Claude Paroz

Triage Stage: AcceptedReady for checkin

comment:5 Changed 5 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 54e2e68:

Fixed #25715 -- Fixed Model.refresh_from_db() with ForeignKey w/on_delete=SET_NULL.

comment:6 Changed 5 years ago by Tim Graham <timograham@…>

In 2179e5a:

[1.8.x] Fixed #25715 -- Fixed Model.refresh_from_db() with ForeignKey w/on_delete=SET_NULL.

Backport of 54e2e688e1cfbdb37dfa5dd3b3ffdf2af12b4423 from master

comment:7 Changed 5 years ago by Tim Graham <timograham@…>

In 8727dc8:

[1.9.x] Fixed #25715 -- Fixed Model.refresh_from_db() with ForeignKey w/on_delete=SET_NULL.

Backport of 54e2e688e1cfbdb37dfa5dd3b3ffdf2af12b4423 from master

Note: See TracTickets for help on using tickets.
Back to Top