Saturday, 15 February 2014

Assign the value of old column to new column using Django data migration -


I get a problem when I wanted to change the type of column from char to int but I could do it directly through the degeneration migration could not do. So I first added a new column and wanted to set its value with the old column, SQL 'update my_app_myqpp set is like UI = ID' How can I do this through 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) Results for the result: result.uuid = str (result.id) result.save () class migration: migrations.Migration: [['myq_app', '0002_auto_20150205_0501' MODEL_NAME = 'MyApp', name = 'UUID', area = 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 also tried

  MyApp.objects.raw ('update Not set docker_build_dockerbuild UUID = ID ')  

but it seemed that the migrate_data was not done. Finally I got my PR contact Oblum Hull way is simple I separated the migration into two steps, One was to add only the new area and the second was to migrate data because the new areas were not included in '0002_auto_20150205_0501' so 'result.save' did not change the result of the non-existent column 'uuide'. UID was defined as a value


No comments:

Post a Comment