[Django] #35416: Removing a primary_key field causes a migration crash

12 views
Skip to first unread message

Django

unread,
Apr 30, 2024, 8:07:39 AMApr 30
to django-...@googlegroups.com
#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.

Django

unread,
Apr 30, 2024, 8:08:42 AMApr 30
to django-...@googlegroups.com
#35416: Removing a primary_key field causes a migration crash
-----------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: dev
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 Sarah Boyce):

* type: Uncategorized => Bug

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

Django

unread,
Apr 30, 2024, 10:23:23 AMApr 30
to django-...@googlegroups.com
#35416: Removing a primary_key field causes a migration crash
-----------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: dev
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
-----------------------------+--------------------------------------
Comment (by Simon Charette):

I would qualify this as a duplicate of #22997 as changing a model from

{{{#!python
class MySimpleModel(models.Model):
some_pk = models.IntegerField(primary_key=True)
}}}

to

{{{#!python
class MySimpleModel(models.Model):
pass
}}}

Is exactly ''removing an explicit primary key''.

The ticket also describes the questioner asking about providing a default
value.

I think that your first instinct was right, this is the same issue which
is not specific to Postgres.
--
Ticket URL: <https://code.djangoproject.com/ticket/35416#comment:2>

Django

unread,
Apr 30, 2024, 10:48:02 AMApr 30
to django-...@googlegroups.com
#35416: Removing a primary_key field causes a migration crash
-----------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: dev
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 Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

Thank you for verifying - duplicate of #22997.
--
Ticket URL: <https://code.djangoproject.com/ticket/35416#comment:3>
Reply all
Reply to author
Forward
0 new messages