From ea2eb666d40c6cd2175058979361e4c10c6e7779 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Thu, 12 Dec 2019 12:40:07 +0300 Subject: [PATCH] fix incorrect removal of uniqueness constraint code field --- .../migrations/0031_auto_20191121_1236.py | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/apps/location/migrations/0031_auto_20191121_1236.py b/apps/location/migrations/0031_auto_20191121_1236.py index da2e96e4..1fb20e14 100644 --- a/apps/location/migrations/0031_auto_20191121_1236.py +++ b/apps/location/migrations/0031_auto_20191121_1236.py @@ -6,6 +6,16 @@ import django.db.models.deletion import utils.models +def copy_country_code(apps, schema_editor): + Country = apps.get_model('location', 'Country') + to_update = [] + for country in Country.objects.all(): + if country.code: + country.code_2 = country.code + to_update.append(country) + Country.objects.bulk_update(to_update, ['code_2', ]) + + class Migration(migrations.Migration): dependencies = [ @@ -63,9 +73,21 @@ class Migration(migrations.Migration): name='region', field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='location.Region', verbose_name='parent region'), ), - migrations.AlterField( + # -- fix incorrect removal of uniqueness constraint code field + migrations.AddField( + model_name='country', + name='code_2', + field=models.CharField(max_length=255, null=True) + ), + migrations.RunPython(copy_country_code, migrations.RunPython.noop), + migrations.RemoveField( model_name='country', name='code', - field=models.CharField(max_length=255, verbose_name='Code'), ), + migrations.RenameField( + model_name='country', + old_name='code_2', + new_name='code', + ) + # fix incorrect removal of uniqueness constraint code field -- ]