Code

#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 21 months ago.
test case

Download all attachments as: .zip

Change History (6)

Changed 21 months ago by dhatch

test case

comment:1 Changed 21 months ago by anonymous

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

comment:2 Changed 21 months ago by dhatch

  • Owner changed from nobody to dhatch

comment:3 Changed 21 months ago by dhatch

  • Cc dhatch added

comment:4 Changed 21 months ago by dhatch

  • Owner changed from dhatch to nobody

comment:5 Changed 21 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.