﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35416	Removing a primary_key field causes a migration crash	Sarah Boyce	nobody	"[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."	Bug	closed	Migrations	dev	Normal	duplicate			Unreviewed	0	0	0	0	0	0
