Opened 7 years ago

Closed 6 years ago

Last modified 4 years ago

#10157 closed (fixed)

Assignment to a reverse OneToOne overwrites related object's PK

Reported by: Anossov Owned by: ianschenck
Component: Database layer (models, ORM) Version: master
Severity: Keywords: onetoone
Cc: dgouldin@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

example models:

class User(models.Model):
    name = models.CharField(max_length=10)
    def __unicode__(self):
        return '<USER: %s>' % self.name
        
class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile', null=True)


When I try to assign a UserProfile instance to a User instance's “profile” attribute, UserProfile's PK AutoField gets overwritten with the User instance:

>>> u = User(name='test')
>>> p = UserProfile()
>>> print p.id, p.user
None None
>>> u.profile = p
>>> print p.id, p.user
<USER test> <USER test>
# Expected: None <USER test>

Attachments (2)

10157.diff (920 bytes) - added by dgouldin 6 years ago.
10157_2.diff (1.3 KB) - added by dgouldin 6 years ago.

Download all attachments as: .zip

Change History (11)

comment:1 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 6 years ago by ianschenck

  • Owner changed from nobody to ianschenck

Changed 6 years ago by dgouldin

comment:3 Changed 6 years ago by dgouldin

  • Cc dgouldin@… added
  • Has patch set

comment:4 Changed 6 years ago by jacob

  • Triage Stage changed from Accepted to Ready for checkin

comment:5 Changed 6 years ago by jacob

  • Triage Stage changed from Ready for checkin to Accepted

Oh, wait, needs a test.

Changed 6 years ago by dgouldin

comment:6 Changed 6 years ago by jacob

  • Triage Stage changed from Accepted to Ready for checkin

comment:7 Changed 6 years ago by jacob

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

(In [10224]) Fixed #10157: correctly set the related objects pk when assigning a reverse OneToOne. Thanks, dgouldin.

comment:8 Changed 6 years ago by jacob

(In [10291]) [1.0.X] Fixed #10157: correctly set the related objects pk when assigning a reverse OneToOne. Thanks, dgouldin.

Backport of r10224 from trunk.

comment:9 Changed 4 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

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