Opened 8 weeks ago

Last modified 8 weeks ago

#29085 new Cleanup/optimization

Possible data loss on .save() with unsaved related model

Reported by: Jonas Haag Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


  • Create unsaved model C
  • Assign unsaved related model P
  • Save related model P and C
  • Relationship is lost

This is similar to #25160 I guess?

I reproduced this with all versions of Django 1.6 up to master.

from django.db import models

class Parent(models.Model):

class Child(models.Model):
    parent = models.ForeignKey(Parent, null=True, blank=True, on_delete=models.CASCADE)
from .models import Parent, Child
from django.test import TestCase

class MyTest(TestCase):
    def test_save(self):
        child = Child()
        child.parent = Parent()
        # This makes the problem go away:
        # child.parent = child.parent

Attachments (1)

patch.diff (2.4 KB) - added by Jonas Haag 8 weeks ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 8 weeks ago by Jonas Haag

Type: UncategorizedCleanup/optimization

comment:2 Changed 8 weeks ago by Tim Graham

Triage Stage: UnreviewedAccepted

Changed 8 weeks ago by Jonas Haag

Attachment: patch.diff added

comment:3 Changed 8 weeks ago by Jonas Haag

Is this the correct approach? Or should we rather consider the behaviour a bug and not use the "prohibited error" in the first place?

comment:4 Changed 8 weeks ago by Tim Graham

Without knowing what an implementation might look like, I'd expect the save to work since the parent is saved before the child.

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