迁移django-model字段名称更改而不会丢失数据 [英] migrating django-model field-name change without losing data

查看:397
本文介绍了迁移django-model字段名称更改而不会丢失数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个django项目,其中的数据库表已经包含数据。我想更改字段名称,而不会丢失该列中的任何数据。我最初的计划是简单地更改模型字段名称,而实际上不会更改数据库表的名称(使用 db_column 列参数):

I have a django project with a database table that already contains data. I'd like to change the field name without losing any of the data in that column. My original plan was to simply change the model field name in a way that would not actually alter the name of the db table (using the db_column column parameter):

原始模型:

class Foo(models.Model):
  orig_name = models.CharField(max_length=50)

新模型:

class Foo(models.Model):
  name = models.CharField(max_length=50, db_column='orig_name')

但是,运行South的 schemamigration --auto 会生成迁移脚本删除原始列 orig_name ,并添加新列 name ,这会产生不必要的副作用删除该列中的数据。 (我对South为什么要更改db中的列名称也感到困惑,因为我对db_column的理解是,它可以更改模型字段名称而无需更改数据库表列的名称)。

But, running South's schemamigration --auto produces a migration script that deletes the original column, orig_name, and adds a new column, name, which would have the unwanted side effect of deleting the data in that column. (I'm also confused as to why South wants to change the name of the column in the db, since my understanding of db_column was that it enables a change to the model field name without changing the name of the database table column).

如果我不能不更改db字段而改变模型字段,我想我可以这样更直接地更改名称:

If I can't get away with changing the model field without changing the db field, I guess I could do a more straight forward name change like so:

原始模型:

class Foo(models.Model):
  orig_name = models.CharField(max_length=50)

新模型:

class Foo(models.Model):
  name = models.CharField(max_length=50)

无论我最终使用哪种策略(我都希望使用第一个策略,但会发现第二个策略可以接受),我主要关心的是确保我不要丢失该列中已经存在的数据。

Regardless of which strategy I end up using (I would prefer the first, but would find the second acceptable), my primary concern is ensuring that I don't lose the data that is already in that column.

这是否需要多步骤的过程? (例如1.添加一列,2.将数据从旧列迁移到新列,并3.删除原始列)
或者我可以使用 db.alter_column ?

Does this require a multi-step process? (such as 1. adding a column, 2. migrating the data from the old column to the new column, and 3. removing the original column) Or can I alter the migration script with something like db.alter_column?

在更改列名的同时保留该列中数据的最佳方法是什么?

What is the best way to preserve the data in that column while changing the column's name?

推荐答案

在保留数据库字段的同时更改字段名称



添加Django 1.8+的答案(使用Django本地迁移,而不是South迁移)。

Changing the field name while keeping the DB field

Adding an answer for Django 1.8+ (with Django-native migrations, rather than South).

进行迁移,首先添加 db_column 属性,然后重命名该字段。 Django理解第一个是无操作(因为它将 db_column 保持不变),第二个是无操作(因为它没有模式变化)。我实际上检查了日志,发现没有架构更改...

Make a migration that first adds a db_column property, and then renames the field. Django understands that the first is a no-op (because it changes the db_column to stay the same), and that the second is a no-op (because it makes no schema changes). I actually examined the log to see that there were no schema changes...

operations = [
    migrations.AlterField(
        model_name='mymodel',
        name='oldname',
        field=models.BooleanField(default=False, db_column=b'oldname'),
    ),
    migrations.RenameField(
        model_name='mymodel',
        old_name='oldname',
        new_name='newname',
    ),
]

这篇关于迁移django-model字段名称更改而不会丢失数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆