Ticket #13987: 13987_fix_2_with_tests.diff

File 13987_fix_2_with_tests.diff, 2.2 KB (added by Aramgutang, 5 years ago)

2nd (superior) fix variant, with regression tests.

  • django/db/models/options.py

     
    113113                # Promote the first parent link in lieu of adding yet another
    114114                # field.
    115115                field = self.parents.value_for_index(0)
     116                already_created = [fld for fld in self.local_fields if fld.name == field.name]
     117                if already_created:
     118                    field = already_created[0]
    116119                field.primary_key = True
    117120                self.setup_pk(field)
    118121            else:
  • tests/regressiontests/model_inheritance_regress/models.py

     
    137137class MessyBachelorParty(BachelorParty):
    138138    pass
    139139
     140# Check concrete -> abstract -> concrete inheritance
     141class SearchableLocation(models.Model):
     142    keywords = models.CharField(max_length=256)
     143
     144class Station(SearchableLocation):
     145    name = models.CharField(max_length=128)
     146   
     147    class Meta:
     148        abstract = True
     149
     150class BusStation(Station):
     151    bus_routes = models.CommaSeparatedIntegerField(max_length=128)
     152    inbound = models.BooleanField()
     153
     154class TrainStation(Station):
     155    zone = models.IntegerField()
     156
    140157__test__ = {'API_TESTS':"""
    141158# Regression for #7350, #7202
    142159# Check that when you create a Parent object with a specific reference to an
     
    386403>>> p4.bachelorparty_set.all()
    387404[<BachelorParty: Bachelor party for Bob>, <BachelorParty: Bachelor party for Dave>]
    388405
     406# Regression for #13987
     407# Check that a primary key is defined for models that inherit from an abstract
     408# model, which in turn inherits from a concrete model.
     409>>> len([field for field in BusStation._meta.local_fields if field.primary_key])
     4101
     411>>> len([field for field in TrainStation._meta.local_fields if field.primary_key])
     4121
     413>>> BusStation._meta.pk.model is BusStation
     414True
     415>>> TrainStation._meta.pk.model is TrainStation
     416True
     417
    389418"""}
    390419
Back to Top