#29085: Possible data loss on .save() with unsaved related model -------------------------------------+------------------------------------- Reporter: Jonas Haag | Owner: nobody Type: | Status: new Cleanup/optimization | Component: Database layer | Version: master (models, ORM) | Severity: Normal | Resolution: Keywords: | Triage Stage: Accepted Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by Ian Foote): I've been looking into this a bit and I don't see how we can do the right thing here. The problem I see is that the {{{Child}}} model has two sources of truth about the {{{Parent}}} instance and we don't know which we can trust. These sources are {{{Child.parent_id}}} and {{{Child.parent.id}}}. When we save the {{{Parent}}} instance, {{{Child.parent.id}}} changes, but {{{Child.parent_id}}} does not and I don't see a way to push the new {{{pk}}} to {{{Child.parent_id}}} behind the scenes. One can reassign {{{Child.parent}}} explicitly (as noted above), which ensures {{{Child.parent_id}}} is correct. I looked into adjusting {{{ForeignKey.pre_save}}} to read from {{{Child.parent}}} and {{{Model.save}}} to avoid clearing {{{Child._state.fields_cache}}}, but this either didn't work or broke many other Django tests. -- Ticket URL: <https://code.djangoproject.com/ticket/29085#comment:7> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/067.35747f5fc9ebda43071e49af7f5adb2c%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.