Change column type with django migrations

╄→гoц情女王★ 提交于 2019-11-28 07:37:52

问题


Consider this structure:

some_table(id: small int)

and I want change it to this:

some_table(id: string)

Now I do this with three migrations:

  1. Create a new column _id with type string
  2. (datamigration) Copy data from id to _id with string conversion
  3. Remove id and rename _id to id

Is there a way to do this with only one migration?


回答1:


You can directly change the type of a column from int to string. Note that, unless strict sql mode is enabled, integers will be truncated to the maximum string length and data is possibly lost, so always make a backup and choose a max_length that's high enough. Also, the migration can't easily be reversed (sql doesn't directly support changing a string column to an int column), so a backup is really important in this one.

Django pre 1.7 / South

You can use db.alter_column. First, create a migration, but don't apply it yet, or you'll lose the data:

>>> python manage.py schemamigration my_app --auto

Then, change the forwards method into this:

class Migration(SchemaMigration):
    def forwards(self, orm):
        db.alter_column('some_table', 'id', models.CharField(max_length=255))

    def backwards(self, orm):
        raise RuntimeError('Cannot reverse this migration.')

This will alter the column to match the new CharField field. Now apply the migration and you're done.

Django 1.7 You can use the AlterField operation to change the column. First, create an empty migration:

>>> python manage.py makemigrations --empty my_app

Then, add the following operation:

class Migration(migrations.Migration):
    operations = [
        migrations.AlterField('some_model', 'id', models.CharField(max_length=255))
    ]

Now run the migration, and Django will alter the field to match the new CharField.



来源:https://stackoverflow.com/questions/27385118/change-column-type-with-django-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!