gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 19:11:42 26 4
gpt4 key购买 nike

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

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

模型.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 无法更改导致 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 中。字段。

最佳答案

好吧,我花了一段时间才算出来:但基本上,我认为你做不到:

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

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

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

如果你想确保数据完整性实际上是一个客户,你可以在保存中放置一个预验证(或者如果你使用某种保存管道的话)

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

关于python - Django + SQLite + ForeignKey ('self' ) = 迁移失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39360059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com