2015-11-30 16 views
5

グーグルでdead-end topicを見つけただけで、私はまだ移行の問題に取り組んでいます。django.core.exceptions.FieldDoesNotExist:モデルには<function SET_NULL at 0x7fc5ae8836e0>というフィールドがありません

マイモデル:

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) 
モデルファイル内

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 

輸入

フルトレースバックmigrateを使用して:

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> 

なぜでしょうCurationArticleでフィールドのmodels.SET_NULLコール?
私はそれが私自身の責任だと確信しています。

+0

'curation_article'関係を含むフルモデルを投稿できますか?そして、私は、 'CurationArticle'は引用符で囲まれているので、同じmodels.pyファイルにあると仮定しています。 – souldeux

+0

完全なトレースバックを投稿できますか? – knbk

+0

@souldeux:はい、同じファイルにあります。私は自分の投稿を更新しました。 –

答えて

3

私はこの問題は、データベースがblogpostテーブルからcurationarticleテーブルに存在する外部キーを参照していないことを意味します。ただし、blogpostには、テーブルのIDの格納に使用されていたcuration_article_idという列があります。今

あなたはDjangoはそれだけでIntegerFieldとしての代わりに、ForeignKeyとしてcuration_article_idを見blogpostテーブルに関する情報をロードするときに、移行を実行してみてください。 Djangoではまだにon_deleteを添付できますが、フィールド自体にはエラーの原因となるSET_NULLメソッドがありません。

解決方法は、データベースのエラーを修正することです。

データベースの異常の原因を突き止めることができれば幸いです。


データベースとは何の関係もなく、中間的な人間としてのPyCharm自体が混乱している可能性があります。

解決策は、データベースをPyCharmから切り離して再接続することです。

+0

あなたは正しいと思われます。私は移行を削除してもう一度追加することで、フィールドを再構築しました。私は 'clear()'を使ってテスト中にForeignKeyをクリアしたかもしれません。ありがとうございました! –

4

私はまったく同じ問題を抱えていました。これは私の問題を解決しました:

on_delete='SET_NULL'をモデル定義で定義した場合、on_delete=models.SET_NULLに置き換えてください。私は、フィールドを削除していた私の場合import django.db.models.deletion

14

: はその後、モジュールをインポートすることを忘れないでください、

field=models.ForeignKey(.... on_delete=b'SET_NULL' .... 

を含む他の移行を探して、また

field=models.ForeignKey(.... on_delete=django.db.models.deletion.SET_NULL .... 

と交換します最初はunique_togetherメタ属性を変更してそこに削除されたフィールドを残すことを忘れてしまった。 Djangoは明らかに不平を言いました。私はunique_togetherを変更しましたが、migrations.AlterUniqueTogetherの前にmigrations.RemoveFieldで移行が行われましたが、このエラーが発生しました。

から移行ファイルの順番を変更した後:に

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

...少し美しさは移行。

誰かを助けることを望む

+0

私は決してこれを理解していないでしょう – user798719

関連する問題