【发布时间】:2015-11-19 23:26:51
【问题描述】:
今晚我向模型添加了一个字段,当我运行 makemigrations 时,它告诉我该字段不存在。我注意到跟踪中引用了以前的迁移。我尝试将另一个字段添加到不同的模型并且 makemigrations 工作正常。
不仅 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)
【问题讨论】:
标签: python django django-models django-migrations