#29574: Unable to create model instance after changing an abstract model to non-
abstract due to "foreign key mismatch" error
----------------------------+-------------------------------------
Reporter: josephbiko | Owner: Calvin Vu
Type: Bug | Status: assigned
Component: Migrations | Version: 2.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+-------------------------------------
Comment (by Calvin Vu):
I have reproduced the {{{ foreign key mismatch - "testapp_c" referencing
"testapp_b" }}} error but with slightly different steps:
1. Add the models to models.py
{{{
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)
}}}
2. Make migrations and migrate
3. Make the A model concrete (by removing its Meta)
{{{
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)
}}}
4. Make migrations which will present the following output:
{{{
It is impossible to add a non-nullable field 'a_ptr' to b without
specifying a default. This is because the database needs something to
populate existing rows.
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a
null value for this column)
2) Quit and manually define a default value in models.py.
Select an option:
}}}
5. Go with option 1 and provide an integer (I provided 0)
6. Migrate and you'll see the error:
{{{
foreign key mismatch - "testapp_c" referencing "testapp_b"
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29574#comment:14>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070191f00a9af7-895ac119-7891-48dc-93d7-e303aa67f173-000000%40eu-central-1.amazonses.com.