Django + SQLite + ForeignKey('self')=迁移失败

时间:2016-09-07 01:39:03

标签: python django sqlite migration

在使用SQLite作为数据库后端的Django 1.9中,在修改模型以使用多表继承而不是之前使用的OneToOneField关系后尝试应用迁移时出现错误。

特别是,问题似乎是由于在模型中包含ForeignKey('self')

models.py

以下是成功完成并应用初始迁移的应用程序:

from django.db import models
from django.contrib.auth.models import User


class Customer(models.Model):
    account = models.OneToOneField(User)
    parent = models.ForeignKey('self', null=True)

models.py(已修改)

然后修改应用程序以从User模型继承而不是链接到它:

from django.db import models
from django.contrib.auth.models import User

class Customer(User):
    parent = models.ForeignKey('self', null=True)

此时./manage.py makemigrations <app>成功,但随后应用迁移失败:

错误

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
    field,
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 221, in add_field
    self._remake_table(model, create_fields=[field])
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 181, in _remake_table
    self.create_model(temp_model)
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 250, in create_model
    to_column = field.remote_field.model._meta.get_field(field.remote_field.field_name).column
  File "/home/piranha/.virtualenvs/Python_3.5-Django_1.9/lib/python3.5/site-packages/django/db/models/options.py", line 582, in get_field
    raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Customer has no field named 'id'

同样,问题似乎只发生在使用SQLite并包含自引用ForeignKey时。我认为它正在发生,因为SQLite不能ALTER列导致Django重建表,但自引用ForeignKey导致重建查找新模型中不再存在的'id'列。

我尝试过各种方式手动调整迁移操作,甚至添加migrations.RunPythonapps.get_model()来使用模型的历史版本。似乎没什么用。

如何调整以下迁移以避免错误:

移植

# -*- coding: utf-8 -*-
# Generated by Django 1.9.2 on 2016-09-07 01:16
from __future__ import unicode_literals

from django.conf import settings
import django.contrib.auth.models
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('base', '0001_initial'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='customer',
            options={'verbose_name': 'user', 'verbose_name_plural': 'users'},
        ),
        migrations.AlterModelManagers(
            name='customer',
            managers=[
                ('objects', django.contrib.auth.models.UserManager()),
            ],
        ),
        migrations.RemoveField(
            model_name='customer',
            name='account',
        ),
        migrations.RemoveField(
            model_name='customer',
            name='id',
        ),
        migrations.AddField(
            model_name='customer',
            name='user_ptr',
            field=models.OneToOneField(auto_created=True, default=None, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL),
            preserve_default=False,
        ),
    ]

而且,是的,我知道我最终需要将迁移分解为多个阶段并进行数据迁移,以便在新的user_ptr字段中添加一些有用的东西。

1 个答案:

答案 0 :(得分:0)

好的,我需要一段时间才能解决问题:但基本上,我认为你不能这样做:

class Customer(User):
    parent = models.ForeignKey('self', null=True)

相反,它应该更像(未经测试的)

class Customer(User):
    parent = models.ForeignKey('admin.User', null=True)

如果您想确保数据完整性实际上是一个客户,您可以在保存中进行预验证(或者,如果您正在使用某种类型的保存管道),

如果包含数据迁移步骤以保留该值,可能还是通过使用临时值将其存储(或转储并重新插入对象)。