Opened 3 years ago

Closed 3 years ago

#18638 closed Bug (worksforme)

Reverse OneToOne relationship does not save properly.

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

Description

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.

Example:

Models:

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

class Data(models.Model):
    pass

Run:

data = Data()
t = Tag.objects.create()
data.tag = t
data.save()
# refresh from db
data = Data.objects.get(pk=data.pk)
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`/..; ./runtests.py --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 dhatch 3 years ago.
test case

Download all attachments as: .zip

Change History (6)

Changed 3 years ago by dhatch

test case

comment:1 Changed 3 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement set

comment:2 Changed 3 years ago by dhatch

  • Owner changed from nobody to dhatch

comment:3 Changed 3 years ago by dhatch

  • Cc dhatch added

comment:4 Changed 3 years ago by dhatch

  • Owner changed from dhatch to nobody

comment:5 Changed 3 years ago by aaugustin

  • Resolution set to worksforme
  • Status changed from new to closed

You're seeing the expected behavior. You'd need to call tag.save().

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