Changes between Initial Version and Version 5 of Ticket #29574


Ignore:
Timestamp:
Jul 19, 2018, 6:10:47 AM (6 years ago)
Author:
josephbiko
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #29574

    • Property Owner nobody removed
    • Property Component UncategorizedMigrations
    • Property Type UncategorizedBug
    • Property Cc josephbiko added
  • Ticket #29574 – Description

    initial v5  
    11Django Foreign key's do not track the models DB when the model is inherited from abstract class.
    22
     3Steps to reproduce:
    34
    4   {{{
    5 class A(Model):
    6    class Meta:
    7       abstract = True
     5create models:"
     6
     7{{{
     8class A(models.Model):
     9    field = models.IntegerField()
     10   
     11    class Meta:
     12        abstract = True
     13
    814class B(A):
    9    pass
    10 class C(Model):
    11    fk = foreignkey(B,on_delete=models.CASCADE)
     15    field2 = models.IntegerField()
     16
     17class C(models.Model):
     18    fk = models.ForeignKey(B,on_delete=models.CASCADE)
    1219}}}
    1320
    14 now model B is in table 1 and C points to table 1.
    15   {{{
    16 class A(Model):
    17    pass
     21migrate
     22
     23change the models (by removeing the meta )to:
     24{{{
     25class A(models.Model):
     26    field = models.IntegerField()
     27
     28
    1829class B(A):
    19    pass
    20 class C(Model):
    21    fk = foreignkey(B,on_delete=models.CASCADE)
     30    field2 = models.IntegerField()
     31
     32class C(models.Model):
     33    fk = models.ForeignKey(B,on_delete=models.CASCADE)
    2234}}}
    23 Now Model B is in table 2 with Model A, however C is still pointing to table 1, which leads to a foreign key mismatch error when you try to add an instance of B
     35
     36run migrations.
     37
     38Now add an object of class B in the admin.
     39
     40resulting error:
     41"foreign key mismatch - "models_c" referencing "models_b""
     42
Back to Top