Django migration converting integer pk table to UUID pk table
| # -*- coding: utf-8 -*- | |
| from __future__ import unicode_literals | |
| import uuid | |
| from django.db import migrations, models | |
| def fill_mymodel_uuid(apps, schema_editor): | |
| db_alias = schema_editor.connection.alias | |
| MyModel = apps.get_model('myapp', 'MyModel') | |
| for obj in MyModel.objects.using(db_alias).all(): | |
| obj.uuid = uuid.uuid4() | |
| obj.save() | |
| class Migration(migrations.Migration): | |
| """ Change model with integer pk to UUID pk. This migration presumes there | |
| are no db constraints (foreign keys) to this table. | |
| Note: this migration is not reversible. See the comment above the | |
| `RemoveField` operation. Further, this migration is possible in part due | |
| to the fact that there are currenty no foreign key restraints to this table. | |
| """ | |
| dependencies = [ | |
| # ... | |
| ] | |
| operations = [ | |
| migrations.AddField( | |
| model_name='mymodel', | |
| name='uuid', | |
| field=models.UUIDField(null=True), | |
| ), | |
| migrations.RunPython(fill_mymodel_uuid, migrations.RunPython.noop), | |
| migrations.AlterField( | |
| model_name='mymodel', | |
| name='uuid', | |
| field=models.UUIDField(default=uuid.uuid4, serialize=False, editable=False, unique=True), | |
| ), | |
| # this RemoveField operation is irreversible, because in order to | |
| # recreate it, the primary key constraint on the UUIDField would first | |
| # have to be dropped. | |
| migrations.RemoveField('MyModel', 'id'), | |
| migrations.RenameField( | |
| model_name='mymodel', | |
| old_name='uuid', | |
| new_name='id' | |
| ), | |
| migrations.AlterField( | |
| model_name='mymodel', | |
| name='id', | |
| field=models.UUIDField(primary_key=True, default=uuid.uuid4, serialize=False, editable=False, unique=True), | |
| ), | |
| ] |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
PFischbeck
commented
Aug 28, 2017
|
Removing the |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
BossGrand
commented
Aug 28, 2017
|
how do you deal with foreign keys? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
dchaplinsky
commented
Mar 4, 2018
|
Thank you bro! |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment
Hide comment
manuganji
May 10, 2018
@PFischbeck: a primary key has to be unique. Don't know how you plan to use it as a primary key without uniqueness constraint.
manuganji
commented
May 10, 2018
|
@PFischbeck: a primary key has to be unique. Don't know how you plan to use it as a primary key without uniqueness constraint. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Removing the
unique=Truein lines 39 and 53 made the migration reversible for me. For some reason, this allows the primary key constraint on the UUIDField to be dropped.