Django makemigrations 1.8.18 ValueError:字段引用的模型的查找失败

时间:2017-05-29 20:53:40

标签: python django

最近将Django从1.7升级到1.8。将PRD DB转储到DEV中。不要关心DB for dev中的任何迁移,所以:

  1. 删除我的应用中的迁移文件夹。
  2. 从django_migrations表中删除所有行。
  3. 好的,一切都很好。现在,我只想为应用程序进行虚假迁移,然后我们离开。所以我从顶级应用程序'网站'开始。

    运行此:

    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
    

    这说app keyedcache没有找到对模型Sector的引用? keyedcache与它有什么关系?

    Keyedcache是​​我安装的应用程序。

    如果我跑:

    python manage.py migrate keyedcache --fake
    

    它没有说明要移植。

    你走了一圈又一圈。

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

    怎么回事?

1 个答案:

答案 0 :(得分:1)

这是一条充满了许多其他人在你面前旅行的陷阱的道路。首先要做的是再次清理迁移。然后清除所有陈旧的package main import ( "fmt" "path/to/your/factory" _ "path/to/custom/pkga" _ "path/to/custom/pkgb" //add more packages ... ) func main() { a, _ := factory.New("pkga.Alpha") b, _ := factory.New("pkgb.Beta") c, _ := factory.New("pkga.Gamma") fmt.Printf("%T %T %T\n", a, b, c) } 文件。这一步非常重要。

之后你需要做

.pyc

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

./manage.py makemigrations myapp