#29790: Migration that switches a model to a UUID primary key fails with
"duplicate
column name: id"
-------------------------------------+-------------------------------------
Reporter: Richard Ebeling | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 2.1
Severity: Normal | Resolution:
Keywords: multiple primary | Triage Stage: Accepted
keys migration id uuid |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Christian Kreuzberger):
Hi!
We are also affected by this bug, see this Github issue:
https://github.com/anx-ckreuzberger/django-rest-passwordreset/issues/8
I basically had a model that had this as primary key:
{{{
class ResetPasswordToken(models.Model):
key = models.CharField(
_("Key"),
max_length=64,
primary_key=True
)
}}}
And I wanted to use a different primary key now:
{{{
class ResetPasswordToken(models.Model):
id = models.AutoField(
primary_key=True
)
# Key field, though it is not the primary key of the model
key = models.CharField(
_("Key"),
max_length=64,
db_index=True,
unique=True
)
}}}
which lead to the following migration (including some "self-written"
runpython stuff to fill the primary keys):
{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
def populate_auto_incrementing_pk_field(apps, schema_editor):
ResetPasswordToken = apps.get_model('django_rest_passwordreset',
'ResetPasswordToken')
# Generate values for the new id column
for i, o in enumerate(ResetPasswordToken.objects.all()):
o.id = i + 1
o.save()
class Migration(migrations.Migration):
dependencies = [
('django_rest_passwordreset', '0001_initial',),
]
operations = [
migrations.AddField(
model_name='resetpasswordtoken',
name='id',
field=models.IntegerField(null=True),
preserve_default=True,
),
migrations.RunPython(
populate_auto_incrementing_pk_field,
migrations.RunPython.noop
),
# add primary key information to id field
migrations.AlterField(
model_name='resetpasswordtoken',
name='id',
field=models.AutoField(primary_key=True, serialize=False)
),
# remove primary key information from 'key' field
migrations.AlterField(
model_name='resetpasswordtoken',
name='key',
field=models.CharField(db_index=True, max_length=64,
unique=True, verbose_name='Key'),
),
]
}}}
Unfortunately, this migration only works with Django 1.11 and 2.0, but not
with the 2.1.1 (and also 2.1.2), where it spits out the following error:
{{{
File "manage.py", line 31, in <module>
execute_from_command_line(sys.argv)
File "\venv\lib\site-packages\django\core\management\__init__.py", line
381, in execute_from_command_line
utility.execute()
File "\venv\lib\site-packages\django\core\management\__init__.py", line
375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "\venv\lib\site-packages\django\core\management\base.py", line 316,
in run_from_argv
self.execute(*args, **cmd_options)
File "\venv\lib\site-packages\django\core\management\base.py", line 353,
in execute
output = self.handle(*args, **options)
File "\venv\lib\site-packages\django\core\management\base.py", line 83,
in wrapped
res = handle_func(*args, **kwargs)
File "\venv\lib\site-
packages\django\core\management\commands\migrate.py", line 203, in handle
fake_initial=fake_initial,
File "\venv\lib\site-packages\django\db\migrations\executor.py", line
117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
fake_initial=fake_initial)
File "\venv\lib\site-packages\django\db\migrations\executor.py", line
147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake,
fake_initial=fake_initial)
File "\venv\lib\site-packages\django\db\migrations\executor.py", line
244, in apply_migration
state = migration.apply(state, schema_editor)
File "\venv\lib\site-packages\django\db\migrations\migration.py", line
124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state,
project_state)
File "\venv\lib\site-
packages\django\db\migrations\operations\fields.py", line 216, in
database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "\venv\lib\site-packages\django\db\backends\base\schema.py", line
523, in alter_field
old_db_params, new_db_params, strict)
File "\venv\lib\site-packages\django\db\backends\postgresql\schema.py",
line 122, in _alter_field
new_db_params, strict,
File "\venv\lib\site-packages\django\db\backends\base\schema.py", line
719, in _alter_field
"columns": self.quote_name(new_field.column),
File "\venv\lib\site-packages\django\db\backends\base\schema.py", line
133, in execute
cursor.execute(sql, params)
File "\venv\lib\site-packages\django\db\backends\utils.py", line 100, in
execute
return super().execute(sql, params)
File "\venv\lib\site-packages\django\db\backends\utils.py", line 68, in
execute
return self._execute_with_wrappers(sql, params, many=False,
executor=self._execute)
File "\venv\lib\site-packages\django\db\backends\utils.py", line 77, in
_execute_with_wrappers
return executor(sql, params, many, context)
File "\venv\lib\site-packages\django\db\backends\utils.py", line 85, in
_execute
return self.cursor.execute(sql, params)
File "\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "\venv\lib\site-packages\django\db\backends\utils.py", line 85, in
_execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: Multiple primary keys for table
«django_rest_passwordreset_resetpasswordtoken» are not allowed.
}}}
I thought I would be able to fix this by changing the order of
creating/deleting the primary key within that migration. Now it works with
Django 2.1, but not with 1.11 (and also not with 2.0), where it spits out
this error:
{{{
Operations to perform:
Apply all migrations: admin, auth, contenttypes,
django_rest_passwordreset, sessions
Running migrations:
Applying django_rest_passwordreset.0002_pk_migration...Traceback (most
recent call last):
File "/venv/lib/python3.6/site-packages/django/db/backends/utils.py",
line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: multiple primary keys for table
"django_rest_passwordreset_resetpasswordtoken" are not allowed
}}}
So essentially I can not have my django package compatible with Django
2.1, as I want to maintain compatibility with the 1.11 LTS.
I also took a quick look at the example of Tim Graham, this seems to be
exactly the same problem.
--
Ticket URL: <https://code.djangoproject.com/ticket/29790#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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.679127da9eddc9460373781f15258b70%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.