#29574: Django Foreign Key Mismatch
----------------------------+--------------------------------------
Reporter: josephbiko | Owner: (none)
Type: Bug | Status: new
Component: Migrations | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by josephbiko):
* cc: josephbiko (added)
* status: closed => new
* resolution: needsinfo =>
Old description:
> Django Foreign key's do not track the models DB when the model is
> inherited from abstract class.
>
> {{{
> class A(Model):
> class Meta:
> abstract = True
> class B(A):
> pass
> class C(Model):
> fk = foreignkey(B,on_delete=models.CASCADE)
> }}}
>
> now model B is in table 1 and C points to table 1.
> {{{
> class A(Model):
> pass
> class B(A):
> pass
> class C(Model):
> fk = foreignkey(B,on_delete=models.CASCADE)
> }}}
> 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
New description:
Django Foreign key's do not track the models DB when the model is
inherited from abstract class.
Steps to reproduce:
create models:"
{{{
class A(models.Model):
field = models.IntegerField()
class Meta:
abstract = True
class B(A):
field2 = models.IntegerField()
class C(models.Model):
fk = models.ForeignKey(B,on_delete=models.CASCADE)
}}}
migrate
change the models (by removeing the meta )to:
{{{
class A(models.Model):
field = models.IntegerField()
class B(A):
field2 = models.IntegerField()
class C(models.Model):
fk = models.ForeignKey(B,on_delete=models.CASCADE)
}}}
run migrations.
Now add an object of class B in the admin.
resulting error:
"foreign key mismatch - "models_c" referencing "models_b""
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29574#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/068.ee3b4d575c3c0fab391fd13caa675d6e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.