From 182301c8df175524f7272b8d6c2d7a8eac1934f5 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 4 Mar 2009 07:24:30 +0000 Subject: [PATCH] [1.0.X] Fixed #10406 -- Fixed some problems with model inheritance and pk fields. Manually specifying both a OneToOneField(parent_link=True) and separate a primary key field was causing invalid SQL to be generated. Thanks to Ramiro Morales for some analysis on this one. Backport of r9971 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9973 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 2 -- .../model_inheritance_regress/models.py | 25 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index ad7c97359efd1..c7445672ee0bd 100644 --- a/django/db/models/base.py +++ b/django/db/models/base.py @@ -98,8 +98,6 @@ def __new__(cls, name, bases, attrs): # Concrete classes... if base in o2o_map: field = o2o_map[base] - field.primary_key = True - new_class._meta.setup_pk(field) else: attr_name = '%s_ptr' % base._meta.module_name field = OneToOneField(base, name=attr_name, diff --git a/tests/regressiontests/model_inheritance_regress/models.py b/tests/regressiontests/model_inheritance_regress/models.py index d2fa4cbf15dff..a1ee6a2d86f3f 100644 --- a/tests/regressiontests/model_inheritance_regress/models.py +++ b/tests/regressiontests/model_inheritance_regress/models.py @@ -43,6 +43,16 @@ class ParkingLot(Place): def __unicode__(self): return u"%s the parking lot" % self.name +class ParkingLot2(Place): + # In lieu of any other connector, an existing OneToOneField will be + # promoted to the primary key. + parent = models.OneToOneField(Place) + +class ParkingLot3(Place): + # The parent_link connector need not be the pk on the model. + primary_key = models.AutoField(primary_key=True) + parent = models.OneToOneField(Place, parent_link=True) + class Supplier(models.Model): restaurant = models.ForeignKey(Restaurant) @@ -293,5 +303,20 @@ def __unicode__(self): >>> DerivedM.objects.all() [] +# Regression tests for #10406 + +# If there's a one-to-one link between a child model and the parent and no +# explicit pk declared, we can use the one-to-one link as the pk on the child. +# The ParkingLot2 model shows this behaviour. +>>> ParkingLot2._meta.pk.name +"parent" + +# However, the connector from child to parent need not be the pk on the child +# at all. +>>> ParkingLot3._meta.pk.name +"primary_key" +>>> ParkingLot3._meta.get_ancestor_link(Place).name # the child->parent link +"parent" + """}