Django 1.8迁移无法将列ID转换为整数 [英] Django 1.8 migration unable to cast column id to integer

查看:75
本文介绍了Django 1.8迁移无法将列ID转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 of the migrations run fine, but there's a hiccup in our of our applications.

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

We got this far in the Postgres migration. (All other apps fully migrated.) All of the migrations ran without incident on 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:

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:

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

This migration won't run: 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'),
        ),
    ]

尝试运行此迁移会导致:

Attempting to run this migration results in:

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

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

The current, fully migrated state of the relevant model tables is:

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表定义为:

Squashing the migrations didn't help. The Postgres database is clean except for table definitions. The relevant Postgres table definitions in their stuck form are:

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

$ b $我基本上没有主意。看来它已经是一个整数了,真的,它还需要Django指定的主键吗?

I'm basically out of ideas. It looks like it's already an integer, and really, what else would it expect a Django-specified primary key to be?

推荐答案

问题是从Process.milestone作为布尔字段迁移到Process.milestone作为外键。 Postgres不会等待迁移因无法广播的数据而失败。

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

这篇关于Django 1.8迁移无法将列ID转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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