professorDante
professorDante

Reputation: 2405

Django makemigrations 1.8.18 ValueError: Lookup failed for model referenced by field

Recently upgraded Django to 1.8 from 1.7. Taken a dump of the PRD DB into DEV. Don't care about any of the migrations in the DB for dev, so:

  1. Delete migrations folders in my apps.
  2. Delete all rows from django_migrations table.

OK, all good. Now, I just want to make a fake migration for the apps, and away we go. So I start with the top level app 'website'.

Running this:

python manage.py makemigrations website

Gives the file:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

dependencies = [
]

operations = [
    migrations.CreateModel(
        name='HistoricalStock',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('period_date', models.DateField()),
            ('close', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('open', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('day_high', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('day_low', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('change', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('average_gain', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('average_loss', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('rsi', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('true_range', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('average_true_range', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('price_average_true_range', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('thirty_day_constant_maturity_vol_skew', models.DecimalField(default=0.0, null=True, max_digits=20, decimal_places=10, blank=True)),
        ],
        options={
            'ordering': ['period_date'],
        },
    ),
    migrations.CreateModel(
        name='Industry',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('group', models.CharField(unique=True, max_length=40, verbose_name=b'name')),
            ('slug', models.SlugField(unique=True)),
        ],
    ),
    migrations.CreateModel(
        name='Sector',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('group', models.CharField(unique=True, max_length=40, verbose_name=b'name')),
            ('slug', models.SlugField(unique=True)),
        ],
    ),
    migrations.CreateModel(
        name='Stock',
        fields=[
            ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
            ('ticker', models.CharField(max_length=20)),
            ('profile', models.TextField()),
            ('name', models.CharField(max_length=40)),
            ('broker_rating', models.DecimalField(default=0.0, null=True, max_digits=10, decimal_places=2, blank=True)),
            ('ranking_industry', models.CharField(max_length=40, null=True, blank=True)),
            ('ranking_industry_upper_percent', models.DecimalField(null=True, max_digits=20, decimal_places=10, blank=True)),
            ('country', models.CharField(max_length=40, null=True, blank=True)),
            ('has_options', models.BooleanField(default=False)),
            ('current_stock_price', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('change', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('change_percent', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('open', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('day_high', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('day_low', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('ex_div_date', models.DateField(null=True, blank=True)),
            ('pay_div_date', models.DateField(null=True, blank=True)),
            ('earnings_date', models.DateField(null=True, blank=True)),
            ('vol', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('pe', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('mkt_cap', models.DecimalField(default=0, max_digits=40, decimal_places=0)),
            ('div_yield', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('div_amount', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('year_high', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('year_low', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('day180pc', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('day360pc', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('instpct', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('insidepct', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('earnings_share', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('fifty_ma', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('two_hundred_ma', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('std_dev', models.DecimalField(default=0.0, max_digits=20, decimal_places=10)),
            ('stats_period', models.IntegerField(default=0)),
            ('last_refreshed', models.DateTimeField(null=True, editable=False, blank=True)),
            ('industry', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, blank=True, to='website.Industry', null=True)),
            ('sector', models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, blank=True, to='website.Sector', null=True)),
        ],
    ),
    migrations.AddField(
        model_name='industry',
        name='sector',
        field=models.ForeignKey(to='website.Sector'),
    ),
    migrations.AddField(
        model_name='historicalstock',
        name='stock',
        field=models.ForeignKey(to='website.Stock'),
    ),
    migrations.AlterUniqueTogether(
        name='historicalstock',
        unique_together=set([('stock', 'period_date')]),
    ),
]

Looks fine, 4 models, with a couple of FK to models in the same file.

Then, run the fake:

python manage.py migrate website --fake

It goes nuts:

ValueError: Lookup failed for model referenced by field keyedcache.Stock.sector: website.Sector

It's saying that the app keyedcache is not finding a reference to the model Sector? What has keyedcache got to do with it?

Keyedcache is an app I have installed.

If I run:

python manage.py migrate keyedcache --fake

It says nothing to migrate.

And round and round you go.

I have done this hundreds of times in Django 1.7 with no problems at all. Something has changed in 1.8 that is causing this.

Whats going on?

Upvotes: 1

Views: 322

Answers (1)

e4c5
e4c5

Reputation: 53734

This is a path full of pitfalls that many others have travelled before you. The first thing to do is to clear up your migrations again. Then clear up all stale .pyc files. This step is very important.

After that you need to do

./manage.py makemigrations myapp

For all your apps. Then only should you run

./manage.py migrate --fake

Upvotes: 1

Related Questions