Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #10406 -- Fixed some problems with model inheritance an…

…d 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
  • Loading branch information...
commit 182301c8df175524f7272b8d6c2d7a8eac1934f5 1 parent dce458c
Malcolm Tredinnick authored
2  django/db/models/base.py
@@ -98,8 +98,6 @@ def __new__(cls, name, bases, attrs):
98 98
                 # Concrete classes...
99 99
                 if base in o2o_map:
100 100
                     field = o2o_map[base]
101  
-                    field.primary_key = True
102  
-                    new_class._meta.setup_pk(field)
103 101
                 else:
104 102
                     attr_name = '%s_ptr' % base._meta.module_name
105 103
                     field = OneToOneField(base, name=attr_name,
25  tests/regressiontests/model_inheritance_regress/models.py
@@ -43,6 +43,16 @@ class ParkingLot(Place):
43 43
     def __unicode__(self):
44 44
         return u"%s the parking lot" % self.name
45 45
 
  46
+class ParkingLot2(Place):
  47
+    # In lieu of any other connector, an existing OneToOneField will be
  48
+    # promoted to the primary key.
  49
+    parent = models.OneToOneField(Place)
  50
+
  51
+class ParkingLot3(Place):
  52
+    # The parent_link connector need not be the pk on the model.
  53
+    primary_key = models.AutoField(primary_key=True)
  54
+    parent = models.OneToOneField(Place, parent_link=True)
  55
+
46 56
 class Supplier(models.Model):
47 57
     restaurant = models.ForeignKey(Restaurant)
48 58
 
@@ -293,5 +303,20 @@ def __unicode__(self):
293 303
 >>> DerivedM.objects.all()
294 304
 [<DerivedM: PK = 44, base_name = b1, derived_name = d1>]
295 305
 
  306
+# Regression tests for #10406
  307
+
  308
+# If there's a one-to-one link between a child model and the parent and no
  309
+# explicit pk declared, we can use the one-to-one link as the pk on the child.
  310
+# The ParkingLot2 model shows this behaviour.
  311
+>>> ParkingLot2._meta.pk.name
  312
+"parent"
  313
+
  314
+# However, the connector from child to parent need not be the pk on the child
  315
+# at all.
  316
+>>> ParkingLot3._meta.pk.name
  317
+"primary_key"
  318
+>>> ParkingLot3._meta.get_ancestor_link(Place).name  # the child->parent link
  319
+"parent"
  320
+
296 321
 """}
297 322
 

0 notes on commit 182301c

Please sign in to comment.
Something went wrong with that request. Please try again.