50

I have two applications (ook and eek say) and I want to use a foreign key to a model in ook from a model in eek. Both are in INSTALLED_APPS with ook first.

In ook.models.py, i have:

class Fubar(models.Model):
    ...

In eek.models.py, I have:

class monkey(models.Model):
    external = models.ForeignKey('ook.Fubar', blank=True, null=True)
    ...

The migration generated is:

class Migration(migrations.Migration):

    dependencies = [
        ('eek', '0002_auto_20151029_1040'),
    ]

    operations = [
        migrations.AlterField(
            model_name='monkey',
            name='external',
            field=models.ForeignKey(blank=True, to='ook.Fubar', null=True),
        ),
    ]

When I run the migration, I get this error:

   ...
   1595             raise ValueError('Foreign Object from and to fields must be
the same non-zero length')
   1596         if isinstance(self.rel.to, six.string_types):
-> 1597             raise ValueError('Related model %r cannot be resolved' % self.rel.to)
   1598         related_fields = []
   1599         for index in range(len(self.from_fields)):
ValueError: Related model u'ook.Fubar' cannot be resolved

What am I doing wrong?

4
  • Are ook and eek included in INSTALLED_APPS in settings.py? Commented Nov 3, 2015 at 10:11
  • @Leistungsabfall: Yes, they are and ook is before eek. Commented Nov 3, 2015 at 10:17
  • 'ook.models.Foobar' maybe? Commented Nov 3, 2015 at 10:29
  • @cdvv7788: It would not make any difference since I added Fubar to the top __init__.py on ook. Commented Nov 3, 2015 at 11:11

9 Answers 9

80

Because You have ForeignKey in operations, You must add a ook to dependencies:

dependencies = [
    ('ook', '__first__'),
    ('eek', '0002_auto_20151029_1040'),
]

Django migrations have two "magic" values:

  • __first__ - get module first migration
  • __latest__ - get module latest migration
Sign up to request clarification or add additional context in comments.

7 Comments

what 'first' means ? I have the same problem but it doesn't working for me
first means find the app where that "model" was originally created, find it..
Why is this not generated automatically?
@Sören It's probably bug. migrations.AlterField on ForeignKey don't generate dependencies (tested on 1.11.4)
what is "ook" and "eek"? "foo" and "bar"?
|
34

Try running migrations one by one for every model.

This way you can debug the app you are facing problem with

python manage.py migrate appmname

Comments

9

I just got the same error, but referring to a model that was declared as part of the same migration. It turned out that the first migrations.CreateModel(...) referred to a not yet declared model. I manually moved this below the declaration of the referred model and then everything worked fine.

Comments

7

In my case, It was the cache and previous migrations that resulted in this error. I removed __pycache__ and migrations folder and then re-run the migrations command and it worked.

Remember, when you'll do python manage.py makemigrations it won't see any new migrations and will console output no changes detected. You'll have to do python manage.py makemigrations your_app_name instead to make things work.

1 Comment

this is the fastest solution for me
3

Depending on your situation, using run_before may resolve your issue.

For me, I hit the error while running Applying admin.0001_initial.. so django admin was not respecting the order of operations. I added this to my migration file that was adding a new user table:

    run_before = [
        ('admin', '__first__'),
    ]

This resolved my issue.

The detail of my specific issue is that django admin's migration depends on the first migration of my app whereas my User is added much later. This means my user table doesn't exist when admin tries to run migrations.

Comments

1

I encountered this error when trying to use a child model of a base model as a foreign key. It makes sense that it didn't work because there's not an id field on the child model. My fix was to use the parent on the key. Unfortunately this was not immediately intuitive and set me back a couple hours.

Comments

0

I have found that it looks like this bug was not fixed yet when you scroll down to the bottom.

Django ValueError: Related model cannot be resolved Bug

I am using 1.11.7, they are talking about 1.9.3.

It worked everything on localhost, but was always failing on Heroku, so I tested all the options/answers above and nothing worked.

Then I have noticed, localhost DB in Admin I had 1 profile created (1 DB record), went to Heroku and DB has 0 records for Profile table so I have added 1, pushed the migration, python manage.py migrate and all it went OK.

That validates that I did not need to change any of those migrations manually that all is working.

Maybe it will help to someone.

migrations

# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2017-11-23 21:26
from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
         ('blog', '0005_blog_author'),
    ]

    operations = [
        migrations.AlterField(
             model_name='blog',
             name='author',

field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, 
to='core.Profile'),
        ),
    ]

Comments

0

order of dependencies is too important.

in your case, ook must be created first then depend eek on it.

dependencies= [
    ('ook', '0001_initial'),
    ('eek', '0002_auto_20151029_1040'),
    ] 

Comments

0

I was getting the same error:

ValueError: Related model 'feed.customuser' cannot be resolved

to solve this,I did py manage.py migratrations app_name and I did same for all the apps separately.

I also did py manage.py migrate app_name separately to all the apps.

this worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.