2015-08-26 58 views
0

我今天晚上向模型添加了一个字段,当我运行makemigrations时,它告诉我该字段不存在。我注意到跟踪中引用了先前的迁移。我尝试添加另一个字段到不同的模型,并使迁移工作正常。Django runserver和makemigration失败,跟踪显示较旧的迁移

makemigrations不仅失败,而且runserver也失败。两者都与之前的迁移一起,追踪中的#37。我试图产生#40的移民。我手动创建了40号迁移,将我的字段放在models.py中,然后成功迁移。 Runserver然后工作得很好。

我想了解为什么之前的迁移导致该模型中的新makemigrations和runserver失败。我已经通过手动创建迁移脚本解决了这个问题,但是它让我担心以前的迁移会导致此问题。为什么新的makemigration尝试错误,因为我试图添加的确切字段不存在?

# Note: Model2 also happens to be my app name and I may have mistakenly put Model2 where I should have put app name. Since I'm struggling to read what is going on, I wasn't sure. 
Unhandled exception in thread started by <function wrapper at 0x1077b3e60> 
Traceback (most recent call last): 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/utils/autoreload.py", line 222, in wrapper 
    fn(*args, **kwargs) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run 
    self.check_migrations() 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 159, in check_migrations 
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS]) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__ 
    self.loader = MigrationLoader(self.connection) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__ 
    self.build_graph() 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/migrations/loader.py", line 173, in build_graph 
    self.load_disk() 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/migrations/loader.py", line 103, in load_disk 
    migration_module = import_module("%s.%s" % (module_name, migration_name)) 
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/Users/my-user-name/Code/my-project-name/my-app/migrations/0037_auto_20150824_0114.py", line 8, in <module> 
    class Migration(migrations.Migration): 
    File "/Users/my-user-name/Code/my-project-name/my-app/migrations/0037_auto_20150824_0114.py", line 32, in Migration 
    field=models.ForeignKey(default=model1_model2_through.objects.all()[0].pk, to='model2.model1_model2_through'), 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/models/query.py", line 177, in __getitem__ 
    return list(qs)[0] 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/models/query.py", line 141, in __iter__ 
    self._fetch_all() 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator 
    for row in compiler.results_iter(): 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter 
    for rows in self.execute_sql(MULTI): 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql 
    cursor.execute(sql, params) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/Users/my-user-name/.virtualenvs/my-project-name/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute 
    return self.cursor.execute(sql, params) 
django.db.utils.ProgrammingError: column model2_model1_model2_through.test_boolean does not exist 
LINE 1: ...nt_date", "model2_model1_model2_through"."deposit_date", "model2_model1... 


class Model1(models.Model): 
    tours = models.ManyToManyField('Model2', through='Model1_Model2_Through') 
    ... 
class Model2(models.Model): 
    ... 
class Model1_Model2_Through(models.Model): 
    test_boolean = models.BooleanField(default=False) 

回答

0

先前的迁移(在这种情况下为37号)有一个查询集用于计算默认值。 Makemigrations在先前的迁移中运行,正因为如此,早期迁移中的查询集代码可能会导致未来的makemigrations尝试失败,因为在较早的时间点上不存在这些字段。

而不是在你的迁移中做这样的事情。

SomeModel.objects.all() 

您应该这样做,只指定该迁移时实际可用的字段。

SomeModel.objects.all().only('field1', 'field2', 'field3')