[Django] #26404: Change of primary key field does not update foreign keys in migration

9 views
Skip to first unread message

Django

unread,
Mar 24, 2016, 7:32:59 AM3/24/16
to django-...@googlegroups.com
#26404: Change of primary key field does not update foreign keys in migration
--------------------------------+--------------------
Reporter: maciej-pawlisz | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------
Changing from:
{{{
class ModelA(models.Model):
new_pk = models.IntegerField()


class ModelB(models.Model):
model_a = models.ForeignKey(ModelA)
}}}

To:

{{{
class ModelA(models.Model):
new_pk = models.IntegerField(primary_key=True)


class ModelB(models.Model):
model_a = models.ForeignKey(ModelA)
}}}

Results in migration:
{{{
class Migration(migrations.Migration):

dependencies = [
('test_app', '0001_initial'),
]

operations = [
migrations.RemoveField(
model_name='modela',
name='id',
),
migrations.AlterField(
model_name='modela',
name='new_pk',
field=models.IntegerField(primary_key=True, serialize=False),
),
]
}}}

ForeignKey `model_a` in `ModelB` is not altered, so table description at
least in sqlite is wrong:
{{{
CREATE TABLE "test_app_modelb" ("id" integer NOT NULL PRIMARY KEY
AUTOINCREMENT, "model_a_id" integer NOT NULL REFERENCES "test_app_modela"
("id"))
}}}
so `connection.check_constraints(['test_app_modelb'])` fails with
exception
{{{
OperationalError: no such column: REFERRED.id
}}}
making it impossible to load fixture data.

Manually adding Operation:
{{{
migrations.AlterField(
model_name='modelb',
name='model_a',
field=models.ForeignKey(on_delete=models.deletion.CASCADE,
to='test_app.ModelA'),
)
}}}
at the end of migration resolves this problem.

--
Ticket URL: <https://code.djangoproject.com/ticket/26404>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 24, 2016, 7:40:45 AM3/24/16
to django-...@googlegroups.com
#26404: Change of primary key field does not update foreign keys in migration
--------------------------------+--------------------------------------
Reporter: maciej-pawlisz | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: 1.9
Severity: Normal | Resolution: duplicate
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 timgraham):

* status: new => closed
* needs_better_patch: => 0
* resolution: => duplicate
* needs_tests: => 0
* needs_docs: => 0


Comment:

Duplicate of #25012

--
Ticket URL: <https://code.djangoproject.com/ticket/26404#comment:1>

Reply all
Reply to author
Forward
0 new messages