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

标签 python django

最近将 Django 从 1.7 升级到 1.8。将 PRD DB 转储到 DEV 中。不关心开发数据库中的任何迁移,因此:

  1. 删除我的应用中的迁移文件夹。
  2. 从 django_migrations 表中删除所有行。

好的,一切都好。现在,我只想为应用程序进行一次假迁移,然后我们就可以开始了。所以我从顶级应用程序“网站”开始。

运行这个:

python manage.py makemigrations website

给出文件:

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

看起来不错,4 个模型,同一个文件中的模型有几个 FK。

然后,运行假的:

python manage.py migrate website --fake

它变得疯狂:

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

这是说应用程序 keyedcache 找不到对模型扇区的引用? keyedcache 与它有什么关系?

Keyedcache 是我安装的一个应用程序。

如果我运行:

python manage.py migrate keyedcache --fake

它没有说明要迁移。

一圈又一圈地走。

我已经在 Django 1.7 中这样做了数百次,没有任何问题。 1.8 中发生了一些变化,导致了这种情况。

发生什么事了?

最佳答案

这是一条充满陷阱的道路,许多人在您之前已经走过了。首先要做的就是再次清理您的迁移。然后清除所有过时的 .pyc 文件。这一步非常重要。

之后你需要做

./manage.py makemigrations myapp

适用于您的所有应用。那么你只应该运行

./manage.py migrate --fake

关于python - Django makemigrations 1.8.18 ValueError : Lookup failed for model referenced by field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44250156/

相关文章:

python - Python 中的继承和组合,不好的做法?

python - 检查 IP 地址是否在给定范围内

python - python opencv示例中 Unresolved 导入

python - 提交 django 表单集时出现 MultiValueDictKeyError

sql - Django 原始查询游标错误

python - 使用 matplotlib 更改 3D 散点图的默认颜色条

python - 'no_text' 和 '500' 作为对我的 Slack Webhook 尝试的响应代码意味着什么?

django - 单个应用程序中的 ValueError ('Related model %r cannot be resolved' % self.remote_field.model)

python - 我可以在 Django 中手动触发信号吗?

python - 如何在 Python 中将 Word 文档转换为非常简单的 html?