Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#24578 closed Bug (fixed)

prepare_database_save breaks some OneToOneField's in 1.8

Reported by: dxiao2003 Owned by: Tim Graham
Component: Database layer (models, ORM) Version: 1.8
Severity: Release blocker Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

With 1.8 the following doesn't work:

# models.py
from django.db import models

class A(models.Model):
    pass

class Aprime(models.Model):

    original = models.OneToOneField(A, primary_key=True)

class B(models.Model):
    aprime = models.ForeignKey(Aprime)

# tests.py

from django.test import TestCase

from models import *

class TestTestCase(TestCase):

    def test_ref(self):
        a_one = A.objects.create()
        a_two = A.objects.create()

        self.assertNotEqual(a_one, a_two)

        aprime = Aprime.objects.create(original=a_one)
        aprime2 = Aprime.objects.create(original=a_two)

        b = B.objects.create(aprime=aprime)

        self.assertEqual(b.aprime, aprime)

        B.objects.update(aprime=aprime2)

        b = B.objects.get(pk=b.id)

        self.assertEqual(b.aprime, aprime2)

This throws a InterfaceError: Error binding parameter 0 - probably unsupported type. when calling B.objects.update(aprime=aprime2).

The bug disappears when overriding prepare_database_save to just return self.pk instead of getattr(self, field.rel.field_name). (This was the behavior as of 1.7 and the bug does not occur there.)

Not sure why prepare_database_save was modified but this bug actually affects a project of mine so would suggest rolling back to the 1.7 version.

Change History (6)

comment:1 Changed 3 years ago by dxiao2003

Component: UncategorizedDatabase layer (models, ORM)
Type: UncategorizedBug

comment:2 Changed 3 years ago by Tim Graham

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:3 Changed 3 years ago by Tim Graham

Owner: changed from nobody to Tim Graham
Status: newassigned

comment:4 Changed 3 years ago by Tim Graham

Has patch: set

comment:5 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In a10b4c0:

Fixed #24578 -- Fixed crash with QuerySet.update() on FK to O2O fields.

Thanks Anssi Kääriäinen for review.

comment:6 Changed 3 years ago by Tim Graham <timograham@…>

In b1dc128a:

[1.8.x] Fixed #24578 -- Fixed crash with QuerySet.update() on FK to O2O fields.

Thanks Anssi Kääriäinen for review.

Backport of 10b4c010ab2cdaa6ba8bfaec3e3540299ea77be from master

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