我今天晚上在模特身上添加了一个字段,当我运行makemigrations时,它告诉我该字段并不存在。我注意到跟踪中引用了先前的迁移。我尝试将另一个字段添加到另一个模型中,使得数字运算正常。
makemigrations不仅失败了,而且runserver也失败了。两者都有先前的迁移,跟踪中的#37。我试图生成迁移#40。我手动创建了40号迁移,将我的字段放在models.py中,然后成功运行迁移。 Runserver然后运行良好。
我试图深入了解为什么之前的迁移导致该模型中的新makemigrations和runserver失败。我通过手动创建迁移脚本解决了这个问题,但它让我担心以前的迁移导致了这个问题。为什么新的移民尝试由于我试图添加不存在的确切字段而导致错误?
# 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 :(得分:0)
A previous migration, number 37 in this case, had a queryset used to calculate default values. Makemigrations runs through previous migrations, and because of this your queryset code in the earlier migration may cause the future makemigrations attempt to fail because of the fields that do not exist at the earlier point in time.
Instead of doing something like this in your migration.
SomeModel.objects.all()
You should do something like this, specifying only fields actually available at that migration.
SomeModel.objects.all().only('field1', 'field2', 'field3')