Django 1.8 migration unable to cast column id to integer

后端 未结 2 1963
無奈伤痛
無奈伤痛 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条回答
  • 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.

    0 讨论(0)
  • 2021-02-12 23:13

    The problem is the migration from Process.milestone as a boolean field to Process.milestone as a foreign key. Postgres doesn't wait for a migration to fail on uncastable data. It wants a rule to alter the table in advance.

    If you don't intend any sort of data migration between two fields, the easiest option is to simply drop and add the field. In this specific case, it would mean changing the operations as follows:

    operations = [
        migrations.RemoveField(
            model_name='process',
            name='milestone'
        ),
        migrations.AddField(
            model_name='process',
            name='milestone',
            field=models.ForeignKey(to='processes.Milestone'),
        ),
        migrations.AlterModelOptions(
            name='process',
            options={'ordering': ['milestone', 'sequence'], 'verbose_name_plural': 'processes'},
        )
    ]
    
    0 讨论(0)
提交回复
热议问题