[Fixed]-Django.core.exceptions.FieldDoesNotExist: model has no field named <function SET_NULL at 0x7fc5ae8836e0>

7πŸ‘

βœ…

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.

πŸ‘€Noah

54πŸ‘

In my case I had removed a field but initially forgotten to change the unique_together Meta attribute leaving the removed field in there. Django obviously complained and I changed the unique_together but the migration came out with the migrations.RemoveField before the migrations.AlterUniqueTogether and I was getting this error.

After changing the order in the migration file from:

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

to:

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

… the little beauty migrated.

Hope it helps someone

πŸ‘€Rich Ross

4πŸ‘

I had the exact same problem. This solved my issue:

If you defined on_delete='SET_NULL' in model definition, replace with on_delete=models.SET_NULL.
Then, look for other migrations which contain

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

and replace with

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

Also, don’t forget to import the module: import django.db.models.deletion

0πŸ‘

Some additional info for future readers of this thread…
The resolution(trick) posted by @Rich Ross works, in in Django 2.0.6, for cases with the following traceback@migrations as well:

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))
πŸ‘€Snidhi Sofpro

Leave a comment