Changes between Initial Version and Version 5 of Ticket #29574
- Timestamp:
- Jul 19, 2018, 6:10:47 AM (6 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #29574
- Property Owner removed
- Property Component Uncategorized → Migrations
- Property Type Uncategorized → Bug
- Property Cc added
-
Ticket #29574 – Description
initial v5 1 1 Django Foreign key's do not track the models DB when the model is inherited from abstract class. 2 2 3 Steps to reproduce: 3 4 4 {{{ 5 class A(Model): 6 class Meta: 7 abstract = True 5 create models:" 6 7 {{{ 8 class A(models.Model): 9 field = models.IntegerField() 10 11 class Meta: 12 abstract = True 13 8 14 class B(A): 9 pass 10 class C(Model): 11 fk = foreignkey(B,on_delete=models.CASCADE) 15 field2 = models.IntegerField() 16 17 class C(models.Model): 18 fk = models.ForeignKey(B,on_delete=models.CASCADE) 12 19 }}} 13 20 14 now model B is in table 1 and C points to table 1. 15 {{{ 16 class A(Model): 17 pass 21 migrate 22 23 change the models (by removeing the meta )to: 24 {{{ 25 class A(models.Model): 26 field = models.IntegerField() 27 28 18 29 class B(A): 19 pass 20 class C(Model): 21 fk = foreignkey(B,on_delete=models.CASCADE) 30 field2 = models.IntegerField() 31 32 class C(models.Model): 33 fk = models.ForeignKey(B,on_delete=models.CASCADE) 22 34 }}} 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 36 run migrations. 37 38 Now add an object of class B in the admin. 39 40 resulting error: 41 "foreign key mismatch - "models_c" referencing "models_b"" 42