【问题标题】:what happens to existing data when we change model Charfield to IntegerField in django?当我们在 django 中将模型 Charfield 更改为 IntegerField 时,现有数据会发生什么?
【发布时间】:2019-09-27 08:26:35
【问题描述】:

当应用以下迁移时,

class Migration(migrations.Migration):
    dependencies = [
        ('outlets', '0009_auto_20190920_1155'),
    ]

    operations = [
        migrations.AlterField(
            model_name='outlet',
            name='country',
            field=models.IntegerField(choices=[(1, 'UAE'), (2, 'India')],
                                      verbose_name='Country'),
        ),
    ]

出现以下错误,

return self.cursor.execute(sql, params)
django.db.utils.DataError: invalid input syntax for integer: "United States"

这是因为我将 CharField 更改为 IntegerField 并且在 db 中已经存在以下数据“美国”,而且我在类似的问题中读到,django 迁移无法处理此类更改。

有什么方法可以在不删除现有数据的情况下执行此操作?

【问题讨论】:

  • 您可以在 django shell 中手动完成,只需获取国家/地区字段的所有条目以及“美国”在哪里放置 1 和“印度”在哪里放置 2 问题已解决。

标签: django django-models django-migrations


【解决方案1】:

如果你想让事情顺利进行,你需要在迁移中集成数据变异

作为样本:

from django.db import migrations, models
import django.db.models.deletion

def migrate_country(apps, schema_editor):
    Outlet = apps.get_model('your_app_name', 'Outlet')
    # You can either do
    Outlet.objects.all().update(
        temp_country= 'what_ever_update you want'
    )
    # Or
    for outlet in Outlet.objects.all():
        outlet.temp_country='your_transformation_result'
        outlet.save() # Please note django will use it's save method. 
                      # Your eventual override will just be ignore

def reverse_country(apps, schema_editor):
    """
    Do want you want when rolling back your migration
    """
    ....

class Migration(migrations.Migration):

    dependencies = [
        ...
    ]

    operations = [
        migrations.AddField(
            model_name='Outlet',
            name='temp_outlet',
            name='country',
            field=models.IntegerField(choices=[(1, 'UAE'), (2, 'India')],
                                      verbose_name='Country'),
        ),
        # Now, you have temp_outlet available on your schema!
        migrations.RunPython(migrate_country, reverse_country),
        # We dump "old" field
        migrations.RemoveField(
            model_name='article',
            name='outlet',
        ),
        # We rename the 'temp' field as excpected!
        migrations.RenameField(
        model_name='Outlet',
            old_name='oldname',
            new_name='newname',
    ),
    ]

【讨论】:

    【解决方案2】:

    您需要在三个单独的迁移中执行此操作。

    首先,将 IntegerField 添加为 null=True 或默认值 0。

    其次,添加一个数据迁移,获取现有 CharField 中的值,查找匹配的整数值,并将其设置为新的 IntegerField。

    第三,删除CharField,重命名IntegerField,去掉default/null。

    【讨论】:

      猜你喜欢
      • 2020-03-21
      • 2016-08-10
      • 2016-09-05
      • 2021-06-03
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多