#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: worksforme
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: => worksforme
* status: new => closed
Comment:
I have tried again, this time on Postgres for `primary_key=False` ->
`primary_key=True` and it worked for me. For example, I started with a
model:
{{{#!python
class MyModel(models.Model):
flag = models.BooleanField()
some_field = models.CharField(max_length=20)
}}}
Updated `some_field` be a `primary_key` - migration operations
{{{#!python
operations = [
migrations.RemoveField(
model_name='mymodel',
name='id',
),
migrations.AlterField(
model_name='mymodel',
name='some_field',
field=models.CharField(max_length=20, primary_key=True,
serialize=False),
),
]
}}}
Migration created and applied successfully, tested result in the shell -
looks good to me.
There is #22997 which covers other cases that you might have encountered.
So far I don't see a reason to have `primary_key` alterations raise an
error as they appear to be supported in at least some cases, with tickets
for some cases where it isn't supported.
In terms of this conversation
https://github.com/django/django/pull/18056#discussion_r1668771284, we can
have composite primary keys raise an error on alteration.
If you disagree, can you give me specific examples?
--
Ticket URL: <https://code.djangoproject.com/ticket/35589#comment:8>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/010701916f687ca2-abcc25ae-7d0c-41c4-aea6-3f18b3be6d0b-000000%40eu-central-1.amazonses.com.