#35416: Removing a primary_key field causes a migration crash
-----------------------------------------+------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
[I am using SQLite]
Create a model with a primary_key field and makemigrations and migrate
{{{#!python
from django.db import models
class MySimpleModel(models.Model):
some_pk = models.IntegerField(primary_key=True)
}}}
Then remove the field and makemigrations
{{{#!python
class MySimpleModel(models.Model):
pass
}}}
This prompts you to supply a default because
{{{
It is impossible to add a non-nullable field 'id' to mysimplemodel without
specifying a default.
}}}
If you supply a default, you get a migration like this:
{{{#!python
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app3', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='mysimplemodel',
name='some_pk',
),
migrations.AddField(
model_name='mysimplemodel',
name='id',
field=models.BigAutoField(auto_created=True, default=1,
primary_key=True, serialize=False, verbose_name='ID'),
preserve_default=False,
),
]
}}}
This crashes when you migrate with:
{{{
django.db.utils.OperationalError: near ")": syntax error
}}}
But if you remove the provided default from the migration and switch the
order so that the field is added before the previous primary_key field is
removed, the migration can be applied.
This is very similar to #22997 and certainly related (they didn't remove
the field but instead altered the field to no longer be a primary_key in
that ticket).
I am not sure whether to class this as a duplicate and require a fix as
part of #22997 - happy to hear opinions here.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35416>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.