2015-08-26 64 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')