Opened 10 years ago

Closed 10 years ago

#21401 closed Bug (duplicate)

Bug when saving an object pointed by another

Reported by: msbrogli@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords:
Cc: msbrogli@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

My models are:

class A(models.Model):
    b = models.ForeignKey('B', null=True)
    name = models.CharField(max_length=255)

class B(models.Model):
    name = models.CharField(max_length=255)

The following code reproduces the problem:

a = A(name="Test A")
a.b = B(name="Test B")

a.b.save()
a.save()

a = A.objects.get(id=a.id)
assert a.b is not None

The assert fails! As I look inside the code, the field b_id is set only when a.b is being set. So, the workaround is:

a = A(name="Test A")
a.b = B(name="Test B")

a.b.save()
a.b = a.b  # workaround
a.save()

a = A.objects.get(id=a.id)
assert a.b is not None

Now it works as expected.

Is it a bug or a feature? I can't find anything in the docs.

If it is a bug, I guess the object should have a list of which objects are pointing to it. So, when it is added, it should updated everyone id field.

Change History (2)

comment:1 by msbrogli@…, 10 years ago

Summary: But when saving an object pointed by anotherBug when saving an object pointed by another

comment:2 by Tim Graham, 10 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #10811

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