グーグルして 行き止まりのトピック を見つけただけなのに、まだ移行の問題が続いています。
私のモデル:
class CurationArticle(models.Model):
title = models.CharField(max_length=150, null=True, blank=True)
description = models.TextField(null=True, blank=True)
link = models.CharField(max_length=255, null=True, blank=True)
author = models.CharField(max_length=150, blank=True, null=True)
author_link = models.CharField(max_length=255, blank=True, null=True)
def __unicode__(self):
return self.title
モデルファイルのインポート:
from Django.template.defaultfilters import date as _date
import datetime
from Django.db import models
from Django.utils.text import slugify
from redactor.fields import RedactorField
データベース:
create TABLE auth_group (
id integer not null default nextval('auth_group_id_seq'::regclass),
name varchar(80) not null,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX auth_group_pkey ON auth_group (id);
CREATE UNIQUE INDEX auth_group_name_key ON auth_group (name);
CREATE INDEX auth_group_name_253ae2a6331666e8_like ON auth_group (name);
create TABLE auth_group_permissions (
id integer not null default nextval('auth_group_permissions_id_seq'::regclass),
group_id integer not null,
permission_id integer not null,
PRIMARY KEY (id),
FOREIGN KEY (group_id) REFERENCES auth_group (id),
FOREIGN KEY (permission_id) REFERENCES auth_permission (id)
);
CREATE UNIQUE INDEX auth_group_permissions_pkey ON auth_group_permissions (id);
CREATE UNIQUE INDEX auth_group_permissions_group_id_permission_id_key ON auth_group_permissions (group_id, permission_id);
CREATE INDEX auth_group_permissions_0e939a4f ON auth_group_permissions (group_id);
CREATE INDEX auth_group_permissions_8373b171 ON auth_group_permissions (permission_id);
create TABLE auth_permission (
id integer not null default nextval('auth_permission_id_seq'::regclass),
name varchar(255) not null,
content_type_id integer not null,
codename varchar(100) not null,
PRIMARY KEY (id),
FOREIGN KEY (content_type_id) REFERENCES Django_content_type (id)
);
CREATE UNIQUE INDEX auth_permission_pkey ON auth_permission (id);
CREATE UNIQUE INDEX auth_permission_content_type_id_codename_key ON auth_permission (content_type_id, codename);
CREATE INDEX auth_permission_417f1b1c ON auth_permission (content_type_id);
create TABLE Django_admin_log (
id integer not null default nextval('Django_admin_log_id_seq'::regclass),
action_time timestamp with time zone not null,
object_id text,
object_repr varchar(200) not null,
action_flag smallint not null,
change_message text not null,
content_type_id integer,
user_id integer not null,
PRIMARY KEY (id),
FOREIGN KEY (content_type_id) REFERENCES Django_content_type (id),
FOREIGN KEY (user_id) REFERENCES main_useraccount (id)
);
CREATE UNIQUE INDEX Django_admin_log_pkey ON Django_admin_log (id);
CREATE INDEX Django_admin_log_417f1b1c ON Django_admin_log (content_type_id);
CREATE INDEX Django_admin_log_e8701ad4 ON Django_admin_log (user_id);
create TABLE Django_content_type (
id integer not null default nextval('Django_content_type_id_seq'::regclass),
app_label varchar(100) not null,
model varchar(100) not null,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX Django_content_type_pkey ON Django_content_type (id);
CREATE UNIQUE INDEX Django_content_type_app_label_45f3b1d93ec8c61c_uniq ON Django_content_type (model, #4);
create TABLE Django_migrations (
id integer not null default nextval('Django_migrations_id_seq'::regclass),
app varchar(255) not null,
name varchar(255) not null,
applied timestamp with time zone not null,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX Django_migrations_pkey ON Django_migrations (id);
create TABLE Django_session (
session_key varchar(40) not null,
session_data text not null,
expire_date timestamp with time zone not null,
PRIMARY KEY (session_key)
);
CREATE UNIQUE INDEX Django_session_pkey ON Django_session (session_key);
CREATE INDEX Django_session_session_key_461cfeaa630ca218_like ON Django_session (session_key);
CREATE INDEX Django_session_de54fa62 ON Django_session (expire_date);
create TABLE main_blogpost (
id integer not null default nextval('main_blogpost_id_seq'::regclass),
title varchar(150) not null,
slug varchar(255),
image varchar(100),
created_at varchar(150),
updated_at varchar(150),
published boolean not null,
author_id integer not null,
open_graph_description text,
content text,
curation_article_id integer,
PRIMARY KEY (id),
FOREIGN KEY (#12) REFERENCES main_curationarticle (id),
FOREIGN KEY (open_graph_description) REFERENCES main_useraccount (id)
);
CREATE UNIQUE INDEX main_blogpost_pkey ON main_blogpost (id);
CREATE UNIQUE INDEX main_blogpost_slug_key ON main_blogpost (slug);
CREATE INDEX main_blogpost_slug_6206488e633eef95_like ON main_blogpost (slug);
CREATE INDEX main_blogpost_4f331e2f ON main_blogpost (open_graph_description);
CREATE INDEX main_blogpost_8093b65d ON main_blogpost (#12);
create TABLE main_blogpost_categories (
id integer not null default nextval('main_blogpost_categories_id_seq'::regclass),
blogpost_id integer not null,
blogpostcategory_id integer not null,
PRIMARY KEY (id),
FOREIGN KEY (blogpostcategory_id) REFERENCES main_blogpostcategory (id)
);
CREATE UNIQUE INDEX main_blogpost_categories_pkey ON main_blogpost_categories (id);
CREATE UNIQUE INDEX main_blogpost_categories_blogpost_id_blogpostcategory_id_key ON main_blogpost_categories (blogpost_id, blogpostcategory_id);
CREATE INDEX main_blogpost_categories_53a0aca2 ON main_blogpost_categories (blogpost_id);
CREATE INDEX main_blogpost_categories_fa55acd5 ON main_blogpost_categories (blogpostcategory_id);
create TABLE main_blogpostcategory (
id integer not null default nextval('main_blogpostcategory_id_seq'::regclass),
name varchar(150) not null,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX main_blogpostcategory_pkey ON main_blogpostcategory (id);
create TABLE main_curationarticle (
id integer not null default nextval('main_curationarticle_id_seq'::regclass),
title varchar(150),
description text,
link varchar(255),
author varchar(150),
author_link varchar(255),
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX main_curationarticle_pkey ON main_curationarticle (id);
create TABLE main_newsletteraccount (
id integer not null default nextval('main_newsletteraccount_id_seq'::regclass),
email varchar(254) not null,
created_at date not null,
is_active boolean not null,
frequency varchar(2) not null,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX main_newsletteraccount_pkey ON main_newsletteraccount (id);
CREATE UNIQUE INDEX main_newsletteraccount_email_key ON main_newsletteraccount (email);
CREATE INDEX main_newsletteraccount_email_71d3d7a4170cd1fe_like ON main_newsletteraccount (email);
create TABLE main_useraccount (
id integer not null default nextval('main_useraccount_id_seq'::regclass),
password varchar(128) not null,
last_login timestamp with time zone,
email varchar(254) not null,
first_name varchar(150) not null,
last_name varchar(150) not null,
"position" varchar(150),
image varchar(100),
bio text,
created_at date not null,
is_active boolean not null,
is_staff boolean not null,
is_admin boolean not null,
is_superuser boolean not null,
PRIMARY KEY (id)
);
CREATE UNIQUE INDEX main_useraccount_pkey ON main_useraccount (id);
CREATE UNIQUE INDEX main_useraccount_email_68fbb6e11cf0e24_uniq ON main_useraccount (email);
create TABLE main_useraccount_groups (
id integer not null default nextval('main_useraccount_groups_id_seq'::regclass),
useraccount_id integer not null,
group_id integer not null,
PRIMARY KEY (id),
FOREIGN KEY (useraccount_id) REFERENCES main_useraccount (id),
FOREIGN KEY (group_id) REFERENCES auth_group (id)
);
CREATE UNIQUE INDEX main_useraccount_groups_pkey ON main_useraccount_groups (id);
CREATE UNIQUE INDEX main_useraccount_groups_useraccount_id_group_id_key ON main_useraccount_groups (useraccount_id, group_id);
CREATE INDEX main_useraccount_groups_f705e196 ON main_useraccount_groups (useraccount_id);
CREATE INDEX main_useraccount_groups_0e939a4f ON main_useraccount_groups (group_id);
create TABLE main_useraccount_user_permissions (
id integer not null default nextval('main_useraccount_user_permissions_id_seq'::regclass),
useraccount_id integer not null,
permission_id integer not null,
PRIMARY KEY (id),
FOREIGN KEY (useraccount_id) REFERENCES main_useraccount (id),
FOREIGN KEY (permission_id) REFERENCES auth_permission (id)
);
CREATE UNIQUE INDEX main_useraccount_user_permissions_pkey ON main_useraccount_user_permissions (id);
CREATE UNIQUE INDEX main_useraccount_user_permissi_useraccount_id_permission_id_key ON main_useraccount_user_permissions (useraccount_id, permission_id);
CREATE INDEX main_useraccount_user_permissions_f705e196 ON main_useraccount_user_permissions (useraccount_id);
CREATE INDEX main_useraccount_user_permissions_8373b171 ON main_useraccount_user_permissions (permission_id);
そのモデルを参照するForeignKey:
class BlogPost(models.Model):
title = models.CharField(max_length=150)
slug = models.CharField(max_length=255, unique=True,null=True, blank=True)
image = models.ImageField(upload_to='blog_post_images', null=True, blank=True)
content = RedactorField(max_length=10000, verbose_name='Redactor', null=True, blank=True)
open_graph_description = models.TextField(null=True, blank=True)
created_at = models.CharField(blank=True, null=True, max_length=150)
updated_at = models.CharField(blank=True, null=True, max_length=150)
author = models.ForeignKey('UserAccount')
categories = models.ManyToManyField('BlogPostCategory')
published = models.BooleanField(default=False)
curation_article = models.ForeignKey('CurationArticle', null=True, blank=True, on_delete=models.SET_NULL)
migrate
使用時の完全なTraceBack:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/Django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/Django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/Django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/Django/core/management/commands/migrate.py", line 221, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/Django/db/migrations/executor.py", line 110, in migrate
self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
File "/usr/local/lib/python2.7/dist-packages/Django/db/migrations/executor.py", line 148, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/local/lib/python2.7/dist-packages/Django/db/migrations/migration.py", line 115, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/usr/local/lib/python2.7/dist-packages/Django/db/migrations/operations/fields.py", line 201, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/usr/local/lib/python2.7/dist-packages/Django/db/backends/base/schema.py", line 456, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/fields/related.py", line 2008, in db_parameters
return {"type": self.db_type(connection), "check": []}
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/fields/related.py", line 1999, in db_type
rel_field = self.related_field
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/fields/related.py", line 1902, in related_field
return self.foreign_related_fields[0]
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/fields/related.py", line 1636, in foreign_related_fields
return Tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/fields/related.py", line 1623, in related_fields
self._related_fields = self.resolve_related_fields()
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/fields/related.py", line 1616, in resolve_related_fields
else self.rel.to._meta.get_field(to_field_name))
File "/usr/local/lib/python2.7/dist-packages/Django/db/models/options.py", line 554, in get_field
raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
Django.core.exceptions.FieldDoesNotExist: CurationArticle has no field named <function SET_NULL at 0x7fc5ae8836e0>
なぜmodels.SET_NULL
CurationArticle
のフィールドを呼び出しますか?
私自身のせいだと確信しています。
問題は、データベースがblogpost
テーブルからcurationarticle
テーブルへの外部キーをもう認識していないことだと思います。ただし、blogpost
には、curationarticle
テーブルのIDを格納するために使用されていたcuration_article_id
という列がまだあります。
Djangoがblogpost
テーブルに関する情報をロードするときに、マイグレーションを実行しようとすると、curation_article_id
はIntegerField
としてではなく、 a ForeignKey
。Djangoでもon_delete
をIntegerField
にアタッチできますが、フィールド自体にはSET_NULL
メソッドがありませんこれによりエラーが発生します。
修正は、データベースエラーを修正することです。
その後、うまくいけば、そもそもデータベースの異常の原因を突き止めることができます。
データベースとは何の関係もない可能性もあり、仲介者としてのPyCharmは混乱している可能性もあります。
そこでの解決策は、データベースをPyCharmから切り離して再接続することです。
私の場合、フィールドを削除しましたが、最初にunique_togetherメタ属性を変更するのを忘れて、削除したフィールドをそこに残しました。 Django明らかに文句を言い、unique_togetherを変更しましたが、移行はmigrations.RemoveField
の前に migrations.AlterUniqueTogether
そして私はこのエラーを受け取りました。
移行ファイルの順序を次のように変更した後:
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-11-04 15:00
from __future__ import unicode_literals
from Django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('team', '0021_auto_20161102_1930'),
]
operations = [
migrations.RemoveField(
model_name='csvideo',
name='youtube',
),
migrations.AlterUniqueTogether(
name='csvideo',
unique_together=set([('casestudy', 'platform', 'video_id')]),
),
]
に:
# -*- coding: utf-8 -*-
# Generated by Django 1.9.1 on 2016-11-04 15:00
from __future__ import unicode_literals
from Django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('team', '0021_auto_20161102_1930'),
]
operations = [
migrations.AlterUniqueTogether(
name='csvideo',
unique_together=set([('casestudy', 'platform', 'video_id')]),
),
migrations.RemoveField(
model_name='csvideo',
name='youtube',
),
]
...小さな美しさが移行しました。
それが誰かを助けることを願って
私はまったく同じ問題を抱えていました。これは私の問題を解決しました:
モデル定義でon_delete='SET_NULL'
を定義した場合は、on_delete=models.SET_NULL
に置き換えます。次に、他のマイグレーションを探します
field=models.ForeignKey( .... on_delete=b'SET_NULL' ....
と置き換える
field=models.ForeignKey( .... on_delete=Django.db.models.deletion.SET_NULL ....
また、モジュールをインポートすることを忘れないでください:import Django.db.models.deletion
このスレッドの将来の読者のためのいくつかの追加情報... @Rich Rossによって投稿された解像度(トリック)は、Django 2.0.6で機能します。以下のtraceback @ migrationsの場合も同様です。 :
File "...\Django\db\migrations\executor.py", line 117, in migrate
File "...\Django\db\migrations\executor.py", line 147, in _migrate_all_forwards
File "...\Django\db\migrations\executor.py", line 244, in apply_migration
File "...\Django\db\migrations\migration.py", line 122, in apply
File "...\Django\db\migrations\operations\models.py", line 525, in database_forwards
File "...\Django\db\backends\base\schema.py", line 358, in alter_unique_together
File "...\Django\db\backends\base\schema.py", line 381, in _delete_composed_index
File "...\Django\db\backends\base\schema.py", line 381, in <listcomp>
File "...\Django\db\models\options.py", line 568, in get_field
raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name))