#35589: AlterField should raise an error when changing primary key
----------------------------------+--------------------------------------
Reporter: Csirmaz Bendegúz | Owner: (none)
Type: New feature | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: primary key | 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: => wontfix
* status: new => closed
Comment:
On SQLite, with a model:
{{{#!python
class PkModel(models.Model):
field_1 = models.CharField(max_length=255, primary_key=True)
field_2 = models.CharField(max_length=255)
}}}
The schema is: `CREATE TABLE "app1_pkmodel" ("field_2" varchar(255) NOT
NULL, "field_1" varchar(255) NOT NULL PRIMARY KEY)`
I remove `primary_key=True` from `field_1` and add it to `field_2` and
`makemigrations`, the operations generated are:
{{{#!python
operations = [
migrations.AlterField(
model_name='pkmodel',
name='field_1',
field=models.CharField(max_length=255),
),
migrations.AlterField(
model_name='pkmodel',
name='field_2',
field=models.CharField(max_length=255, primary_key=True,
serialize=False),
),
]
}}}
This migrates successfully and the schema then becomes: `CREATE TABLE
"app1_pkmodel" ("field_1" varchar(255) NOT NULL, "field_2" varchar(255)
NOT NULL PRIMARY KEY)`
I can also migrate backwards successfully and the schema changes back. In
the shell, I can create (or not create) data as expected.
Given this, altering a primary key is at the very least supported on
SQLite.
Csirmaz, you might have found a specific bug or a backend where this is
not supported. We would need more details to replicate this
Given that we support it in some cases, and are actively working on trying
to resolve bugs in specific cases (#22997), I think the path forward would
be sharing the case you found where it doesn't work and that would be
treated as a bug
--
Ticket URL: <
https://code.djangoproject.com/ticket/35589#comment:4>