Django 1.8 迁移无法将列 ID 转换为整数

Posted

技术标签:

【中文标题】Django 1.8 迁移无法将列 ID 转换为整数【英文标题】:Django 1.8 migration unable to cast column id to integer 【发布时间】:2015-09-06 01:00:07 【问题描述】:

我正在将我的网站从 SQLite 后端迁移到 Postgres 后端。从项目一开始,我们就一直在运行原生 Django 风格的迁移(即不是 South)。大多数迁移运行良好,但我们的应用程序出现了问题。

我们在 Postgres 迁移中走到了这一步。 (所有其他应用程序已完全迁移。)所有迁移都在 SQLite3 上顺利运行。

processes
 [X] 0001_initial
 [X] 0002_auto_20150508_2149
 [ ] 0003_auto_20150511_1543
 [ ] 0004_auto_20150528_1739
 [ ] 0005_process_upstream
 [ ] 0006_auto_20150605_1436
 [ ] 0007_auto_20150605_1706
 [ ] 0008_milestone_prevailing_process

这两个迁移运行正确:

0001_initial.py:

class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='DateReason',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(unique=True, max_length=50)),
                ('active', models.BooleanField(default=True)),
                ('final', models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            name='EventType',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(unique=True, max_length=50)),
                ('active', models.BooleanField(default=True)),
            ],
        ),
        migrations.CreateModel(
            name='Metric',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(unique=True, max_length=50)),
                ('active', models.BooleanField(default=True)),
            ],
        ),
        migrations.CreateModel(
            name='Process',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=50)),
                ('sequence', models.PositiveIntegerField()),
                ('sla', models.PositiveSmallIntegerField(null=True, blank=True)),
                ('milestone', models.BooleanField(default=False)),
            ],
            options=
                'ordering': ['workflow', 'sequence'],
            ,
        ),
        migrations.CreateModel(
            name='Workflow',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(unique=True, max_length=20)),
                ('active', models.BooleanField(default=True)),
            ],
        ),
        migrations.AddField(
            model_name='process',
            name='workflow',
            field=models.ForeignKey(to='processes.Workflow'),
        ),
        migrations.AlterUniqueTogether(
            name='process',
            unique_together=set([('workflow', 'name'), ('workflow', 'sequence')]),
        ),
    ]

0002_auto_20150508_2149.py:

class Migration(migrations.Migration):

    dependencies = [
        ('processes', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Milestone',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=50)),
                ('sequence', models.PositiveIntegerField()),
                ('workflow', models.ForeignKey(to='processes.Workflow')),
            ],
            options=
                'ordering': ['workflow', 'sequence'],
            ,
        ),
        migrations.AlterModelOptions(
            name='process',
            options='ordering': ['milestone', 'sequence'],
        ),
        migrations.AlterUniqueTogether(
            name='process',
            unique_together=set([('milestone', 'name'), ('milestone', 'sequence')]),
        ),
        migrations.RemoveField(
            model_name='process',
            name='workflow',
        ),
        migrations.AlterUniqueTogether(
            name='milestone',
            unique_together=set([('workflow', 'name'), ('workflow', 'sequence')]),
        ),
    ]

此迁移不会运行: 0003_auto_20150511_1543.py

class Migration(migrations.Migration):

    dependencies = [
        ('processes', '0002_auto_20150508_2149'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='process',
            options='ordering': ['milestone', 'sequence'], 'verbose_name_plural': 'processes',
        ),
        migrations.AlterField(
            model_name='process',
            name='milestone',
            field=models.ForeignKey(to='processes.Milestone'),
        ),
    ]

尝试运行此迁移会导致:

django.db.utils.ProgrammingError: column "milestone_id" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.

相关模型表的当前完全迁移状态是:

class Milestone(models.Model):
    """A collection of steps in a workflow"""
    workflow = models.ForeignKey(Workflow, blank=False, null=False)
    name = models.CharField(max_length=50, blank=False, null=False)
    sequence = models.PositiveIntegerField(blank=False, null=False)
    prevailing_process = models.ForeignKey('Process', blank=False, null=True, related_name='controls_milestone')

    class Meta:
        ordering = ['workflow', 'sequence']
        unique_together = (("workflow", "sequence"), ("workflow", "name"))

    def __unicode__(self):
        return u"0: 1".format(self.workflow.name, self.name)


class Process(models.Model):
    """A step in a workflow"""
    milestone = models.ForeignKey(Milestone, blank=False, null=False)
    name = models.CharField(max_length=50, blank=False, null=False)
    sequence = models.PositiveIntegerField(blank=False, null=False)
    sla = models.PositiveSmallIntegerField(blank=True, null=True)
    upstream = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL, related_name='downstream_set')

    class Meta:
        ordering = ['milestone', 'sequence']
        unique_together = (("milestone", "sequence"), ("milestone", "name"))
        verbose_name_plural = "processes"

    def __unicode__(self):
        return u"0 1: 2".format(self.milestone.workflow.name, self.milestone.name, self.name)

压缩迁移没有帮助。 Postgres 数据库是干净的,除了表定义。其卡住形式的相关 Postgres 表定义是:

scorecard=# \d processes_milestone
                                   Table "public.processes_milestone"
   Column    |         Type          |                            Modifiers
-------------+-----------------------+------------------------------------------------------------------
 id          | integer               | not null default nextval('processes_milestone_id_seq'::regclass)
 name        | character varying(50) | not null
 sequence    | integer               | not null
 workflow_id | integer               | not null
Indexes:
    "processes_milestone_pkey" PRIMARY KEY, btree (id)
    "processes_milestone_workflow_id_21e7e70ae59594a8_uniq" UNIQUE CONSTRAINT, btree (workflow_id, sequence)
    "processes_milestone_workflow_id_363216929a08f11e_uniq" UNIQUE CONSTRAINT, btree (workflow_id, name)
    "processes_milestone_846c77cf" btree (workflow_id)
Check constraints:
    "processes_milestone_sequence_check" CHECK (sequence >= 0)
Foreign-key constraints:
    "processes_workflow_id_53b7557aa3f3378e_fk_processes_workflow_id" FOREIGN KEY (workflow_id) REFERENCES processes_workflow(id) DEFERRABLE INITIALLY DEFERRED

scorecard=# \d processes_process
                                  Table "public.processes_process"
  Column   |         Type          |                           Modifiers
-----------+-----------------------+----------------------------------------------------------------
 id        | integer               | not null default nextval('processes_process_id_seq'::regclass)
 name      | character varying(50) | not null
 sequence  | integer               | not null
 sla       | smallint              |
 milestone | boolean               | not null
Indexes:
    "processes_process_pkey" PRIMARY KEY, btree (id)
    "processes_process_milestone_20dc77c2825fcc38_uniq" UNIQUE CONSTRAINT, btree (milestone, name)
    "processes_process_milestone_5bb869985140bf86_uniq" UNIQUE CONSTRAINT, btree (milestone, sequence)
Check constraints:
    "processes_process_sequence_check" CHECK (sequence >= 0)
    "processes_process_sla_check" CHECK (sla >= 0)
Referenced by:
    TABLE "collection_implementation" CONSTRAINT "collection__process_id_6461d2ef37b3f126_fk_processes_process_id" FOREIGN KEY (process_id) REFERENCES processes_process(id) DEFERRABLE INITIALLY DEFERRED

我基本上没有想法。看起来它已经是一个整数了,实际上,Django 指定的主键还能是什么?

【问题讨论】:

fwiw,在某些情况下,我必须先删除一个字段,然后在后续迁移中添加不同类型的字段。 这不是一个干净的解决方案,但是有罪的迁移似乎很小,也许您可​​以手动更改数据库,然后伪造迁移? 继 Brandon 的评论之后,我通过删除该字段然后在后续迁移中再次添加相同的字段来修复它。 【参考方案1】:

问题是从作为布尔字段的 Process.milestone 迁移到作为外键的 Process.milestone。 Postgres 不会等待迁移在不可转换的数据上失败。它想要一个规则来提前改变表格。

如果您不打算在两个字段之间进行任何类型的数据迁移,最简单的选择是简单地删除并添加该字段。在这种特定情况下,这意味着将操作更改如下:

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',
    )
]

【讨论】:

【参考方案2】:

Postgres 不知道如何将布尔字段转换为整数,即使表是空的。你需要用 using 子句告诉它。在转换为外键之前,您可以使用 SQL 迁移进行整数转换。我不认为没有任何 SQL 就可以做到这一点,django 不知道该怎么做。

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'),
        ),
    ]

如果表是空的,你也许可以用一个空的 using 子句或一个空的 case 来逃避。

【讨论】:

将 USING 子句添加到 RunSQL 操作并没有解决问题(它仍然抛出相同的错误),但试图找出它出错的地方至少让我更好地了解Django 文档自己解决问题。非常感谢! 错误是什么?你的 USING 子句是什么? 错误是完全一样的。我尝试了一个空的 USING 子句并将其设置为 null 为真和假。 @AlexH。什么是“django 文档中更好的部分”:S 谢谢

以上是关于Django 1.8 迁移无法将列 ID 转换为整数的主要内容,如果未能解决你的问题,请参考以下文章

Django 1.7 和 1.8 之间迁移行为的变化

bind_rows_(x, .id) 中的错误:无法将列从因子转换为数字

django 1.8 测试模型和迁移

postgres 的 Django 1.8 迁移错误

Django 1.8:删除迁移文件夹后未检测到迁移

Django 1.9 迁移问题