我有些困扰我的事情,让我想知道我是否真的理解django。
首先,我在模型中改变了一点:
article_country = models.ManyToManyField(Country, verbose_name=_('Country of the article'),
blank=True, null=True,
related_name='regions_press_article')
相关名称变为:
related_name='country_press_article')
当我尝试迁移时,我有这个可爱的错误:
Unapplying djangocms_press.0002_auto_20160229_1136...Traceback (most recent call last):
File "./manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/var/www/webapps/lib/python3.4/site-packages/raven/contrib/django/management/__init__.py", line 41, in new_execute
return original_func(self, *args, **kwargs)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/var/www/webapps/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/executor.py", line 70, in migrate
self.unapply_migration(migration, fake=fake)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/executor.py", line 122, in unapply_migration
migration.unapply(project_state, schema_editor)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/migration.py", line 157, in unapply
operation.database_backwards(self.app_label, schema_editor, from_state, to_state)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 144, in database_backwards
self.database_forwards(app_label, schema_editor, from_state, to_state)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 460, in alter_field
return self._alter_many_to_many(model, old_field, new_field, strict)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 759, in _alter_many_to_many
new_field.rel.through._meta.get_field_by_name(new_field.m2m_reverse_field_name())[0],
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 470, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 478, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/schema.py", line 886, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "/var/www/webapps/lib/python3.4/site-packages/django/db/backends/postgresql_psycopg2/introspection.py", line 149, in get_constraints
"foreign_key": tuple(used_cols[0].split(".", 1)) if kind.lower() == "foreign key" else None,
IndexError: list index out of range
所以我就像“好吧,不要介意它不重要,让我们进入下一个任务,只需在模型中添加一个新的Charfield:
global_name = models.CharField(_('Global Name'), max_length=310, null=True)
(我确切地说,我撤消了我的国家/地区更改并删除了与其关联的迁移文件)
发生了什么? 使用migrate命令显示相同的错误。
为什么在模型中进行这么少的修改会如此复杂?怎么解决?
哦,顺便说一下,我已经通过执行./manage.py迁移myapp零来解决这类问题。
但这次我不想删除我的应用数据库!