Django 1.8 migration unable to cast column id to integer

后端 未结 2 1951
無奈伤痛
無奈伤痛 2021-02-12 22:56

I\'m migrating my site from an SQLite backend to a Postgres backend. We\'ve been running native-Django style migrations (i.e., not South) from the beginning of the project. Most

2条回答
  •  梦毁少年i
    2021-02-12 23:12

    Postgres doesn't know how to convert a boolean field into an integer, even if the table is empty. You need to tell it with a using clause. You can use an SQL migration for the integer conversion before you convert to a foreign key. I don't think there's a way to do this without any SQL, django doesn't know how to do that.

    ALTER_SQL = '''
        ALTER TABLE processes_process ALTER COLUMN milestone TYPE integer USING (
            CASE milestone
                when TRUE then ...
                when FALSE then ...
            END
            );
        '''
    
    class Migration(migrations.Migration):
    
        dependencies = [
            ('processes', '0002_auto_20150508_2149'),
        ]
    
        operations = [
            migrations.AlterModelOptions(
                name='process',
                options={'ordering': ['milestone', 'sequence'], 'verbose_name_plural': 'processes'},
            ),
            migrations.RunSQL(ALTER_SQL, None, [
                migrations.AlterField(
                    model_name='process',
                    name='milestone',
                    field=models.IntegerField(),
                    preserve_default=True,
                ),
            ]),
            migrations.AlterField(
                model_name='process',
                name='milestone',
                field=models.ForeignKey(to='processes.Milestone'),
            ),
        ]
    

    If the table is empty, you might be able to get away with an empty using clause or an empty case.

提交回复
热议问题