Raphaël Gomès Raphaël Gomès - 29 days ago 13
Python Question

django.core.exceptions.FieldDoesNotExist: model has no field named <function SET_NULL at 0x7fc5ae8836e0>

After some googling and only finding a dead-end topic, I'm still stuck on a migration problem.

My model :

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


The imports in the model file :

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


The database :

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);


The ForeignKey referencing that model :

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)


The full TraceBack when using
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>


Why would
models.SET_NULL
call for a field in
CurationArticle
?

I'm pretty sure it's my own fault.

Answer

I think the problem is the database no longer sees a foreign key existing from the blogpost table to the curationarticle table. However, blogpost still has a column called curation_article_id which used to be used to store the id of the curationarticle table.

Now when you try to run the migration, when Django loads information about the blogpost table it only sees curation_article_id as an IntegerField instead of as a ForeignKey. Django still allows you to attach on_delete to the IntegerField but the field itself does not have a SET_NULL method which causes the error.

The fix would be to correct the database error.

Then hopefully you can figure out what caused the database anomaly in the first place.


There is a chance also that it has nothing to do with the database and that PyCharm as the middle man has confused itself.

The solution there might be to detach the database from PyCharm and reconnect.

Comments