Opened 9 years ago

Closed 9 years ago

Last modified 9 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 by Tim Graham, 9 years ago

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 by Anderson Resende, 9 years ago

Owner: changed from nobody to Anderson Resende
Status: newassigned

comment:3 by Tim Graham, 9 years ago

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 by Claude Paroz, 9 years ago

Triage Stage: AcceptedReady for checkin

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

Resolution: fixed
Status: assignedclosed

In 54e2e68:

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

comment:6 by Tim Graham <timograham@…>, 9 years ago

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 by Tim Graham <timograham@…>, 9 years ago

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