Opened 6 years ago

Closed 6 years ago

#18638 closed Bug (worksforme)

Reverse OneToOne relationship does not save properly.

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


When the reverse side of a one-to-one relationship is assigned to and the model is saved, the association is not properly written to the database.



class Tag(models.Model):
    data = models.OneToOneField("Data")

class Data(models.Model):


data = Data()
t = Tag.objects.create()
data.tag = t
# refresh from db
data = Data.objects.get(
data.tag # raises DoesNotExist

Attached is a patch including a test case which illustrates this issue.
To run the test case cd to the tests directory then...

export PYTHONPATH=`pwd`:`pwd`/..; ./ --settings=test_sqlite one_to_one_regress

test_reverse_one_to_one_save should fail.

Attachments (1)

18638-test.patch (1.1 KB) - added by David Hatch 6 years ago.
test case

Download all attachments as: .zip

Change History (6)

Changed 6 years ago by David Hatch

Attachment: 18638-test.patch added

test case

comment:1 Changed 6 years ago by anonymous

Patch needs improvement: set

comment:2 Changed 6 years ago by David Hatch

Owner: changed from nobody to David Hatch

comment:3 Changed 6 years ago by David Hatch

Cc: David Hatch added

comment:4 Changed 6 years ago by David Hatch

Owner: changed from David Hatch to nobody

comment:5 Changed 6 years ago by Aymeric Augustin

Resolution: worksforme
Status: newclosed

You're seeing the expected behavior. You'd need to call

The API for 1-to-1 relationships isn't totally symmetrical -- after all it isn't at the SQL level.

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