Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13987 -- Ensure that the primary key is set correctly for all …

…models that have concrete-abstract-concrete inheritance, not just the first model. Thanks to Aramgutang for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15498 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 492b8a0821b4cd66a33b5ad8d18234ddcba947f4 1 parent 9ae2a94
Russell Keith-Magee authored February 12, 2011
6  django/db/models/options.py
@@ -123,6 +123,12 @@ def _prepare(self, model):
123 123
                 # Promote the first parent link in lieu of adding yet another
124 124
                 # field.
125 125
                 field = self.parents.value_for_index(0)
  126
+                # Look for a local field with the same name as the
  127
+                # first parent link. If a local field has already been
  128
+                # created, use it instead of promoting the parent
  129
+                already_created = [fld for fld in self.local_fields if fld.name == field.name]
  130
+                if already_created:
  131
+                    field = already_created[0]
126 132
                 field.primary_key = True
127 133
                 self.setup_pk(field)
128 134
             else:
17  tests/regressiontests/model_inheritance_regress/models.py
@@ -146,3 +146,20 @@ class BachelorParty(AbstractEvent):
146 146
 
147 147
 class MessyBachelorParty(BachelorParty):
148 148
     pass
  149
+
  150
+# Check concrete -> abstract -> concrete inheritance
  151
+class SearchableLocation(models.Model):
  152
+    keywords = models.CharField(max_length=256)
  153
+
  154
+class Station(SearchableLocation):
  155
+    name = models.CharField(max_length=128)
  156
+
  157
+    class Meta:
  158
+        abstract = True
  159
+
  160
+class BusStation(Station):
  161
+    bus_routes = models.CommaSeparatedIntegerField(max_length=128)
  162
+    inbound = models.BooleanField()
  163
+
  164
+class TrainStation(Station):
  165
+    zone = models.IntegerField()
24  tests/regressiontests/model_inheritance_regress/tests.py
@@ -11,7 +11,7 @@
11 11
     ParkingLot2, ParkingLot3, Supplier, Wholesaler, Child, SelfRefParent,
12 12
     SelfRefChild, ArticleWithAuthor, M2MChild, QualityControl, DerivedM,
13 13
     Person, BirthdayParty, BachelorParty, MessyBachelorParty,
14  
-    InternalCertificationAudit)
  14
+    InternalCertificationAudit, BusStation, TrainStation)
15 15
 
16 16
 
17 17
 class ModelInheritanceTest(TestCase):
@@ -358,7 +358,7 @@ def test_abstract_base_class_m2m_relation_inheritance(self):
358 358
         parties = list(p4.bachelorparty_set.all())
359 359
         self.assertEqual(parties, [bachelor, messy_parent])
360 360
 
361  
-    def test_11369(self):
  361
+    def test_abstract_verbose_name_plural_inheritance(self):
362 362
         """
363 363
         verbose_name_plural correctly inherited from ABC if inheritance chain
364 364
         includes an abstract model.
@@ -386,3 +386,23 @@ def test_inherited_nullable_exclude(self):
386 386
             ],
387 387
             attrgetter("pk")
388 388
         )
  389
+
  390
+    def test_concrete_abstract_concrete_pk(self):
  391
+        """
  392
+        Primary key set correctly with concrete->abstract->concrete inheritance.
  393
+        """
  394
+        # Regression test for #13987: Primary key is incorrectly determined
  395
+        # when more than one model has a concrete->abstract->concrete
  396
+        # inheritance hierarchy.
  397
+        self.assertEquals(
  398
+            len([field for field in BusStation._meta.local_fields
  399
+                       if field.primary_key]),
  400
+            1
  401
+        )
  402
+        self.assertEquals(
  403
+            len([field for field in TrainStation._meta.local_fields
  404
+                       if field.primary_key]),
  405
+            1
  406
+        )
  407
+        self.assertIs(BusStation._meta.pk.model, BusStation)
  408
+        self.assertIs(TrainStation._meta.pk.model, TrainStation)

0 notes on commit 492b8a0

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