Assign the value of old column to new column using Django data migration -
i got problem when wanted change column's type int char. could't directly via django migrations. firstly added new column , wanted set value old column's, sql 'update my_app_myqpp set uuid=id'. how can via django migrations? thanks!
def migrate_data(apps, schema_editor): myapp = apps.get_model('my_app', 'myapp') db_alias = schema_editor.connection.alias results = myapp.objects.using(db_alias) result in results: result.uuid = str(result.id) result.save() class migration(migrations.migration): dependencies = [ ('myq_app', '0002_auto_20150205_0501'), ] operations = [ migrations.addfield( model_name='myapp', name='uuid', field=api.fields.uuidfield(auto_created=true, default='0', editable=false, max_length=32, unique=true, verbose_name='uuid'), preserve_default=true, ), migrations.runpython(migrate_data) ]
i tried
myapp.objects.raw('update docker_build_dockerbuild set uuid=id')
but seemed migrate_data not performed.
finally got problem solved. way simple. separated migrations 2 steps, 1 add new field , other 1 migrate data. reason '0002_auto_20150205_0501' didn't include new field. 'result.save' didn't update non-exist column 'uuid' result.uuid set value
Comments
Post a Comment