Django runserver 和 makemigration 失败,跟踪显示较旧的迁移

Django runserver and makemigration fail with trace showing older migration

我今天晚上向模型添加了一个字段,当我 运行 makemigrations 时它告诉我该字段不存在。我注意到跟踪中引用了以前的迁移。我尝试将另一个字段添加到不同的模型并且 makemigrations 工作正常。

不仅 makemigrations 失败了,runserver 也失败了。两者都具有先前的迁移,跟踪中的 #37。我试图生成迁移 #40。我手动创建迁移编号40,将我的字段放入models.py,运行迁移成功。然后 Runserver 也工作正常。

我试图深入了解为什么之前的迁移会导致该模型和运行服务器中的新 makemigrations 失败。我已经通过手动创建迁移脚本解决了这个问题,但让我担心的是之前的迁移导致了这个问题。为什么新的 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)

之前的迁移(在本例中为 37)有一个用于计算默认值的查询集。 Makemigrations 在之前的迁移中运行,因此您在早期迁移中的查询集代码可能会导致未来的 makemigrations 尝试失败,因为在较早的时间点不存在的字段。

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

SomeModel.objects.all()

您应该这样做,仅指定在该迁移中实际可用的字段。

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