Last active
August 6, 2024 20:14
Revisions
-
smcoll revised this gist
Aug 27, 2019 . 1 changed file with 1 addition and 8 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -16,10 +16,6 @@ def fill_mymodel_uuid(apps, schema_editor): 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. """ dependencies = [ @@ -38,9 +34,6 @@ class Migration(migrations.Migration): name='uuid', field=models.UUIDField(default=uuid.uuid4, serialize=False, editable=False, unique=True), ), migrations.RemoveField('MyModel', 'id'), migrations.RenameField( model_name='mymodel', @@ -50,6 +43,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='mymodel', name='id', field=models.UUIDField(primary_key=True, default=uuid.uuid4, serialize=False, editable=False), ), ] -
smcoll created this gist
Mar 9, 2016 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ # -*- 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), ), ]