我有一个简单的模型,定义为(sample_app/models.py):
class ABC(models.Model):
field1 = models.CharField(max_length=255, db_index=True)
field2 = models.CharField(max_length=255)
然后我跑
python3 manage.py makemigration sample_app
迁移已创建。运行
python3 manage.py migrate sample_app
迁移已成功执行。
我登录到 mongo shell 并检查了索引:
db.collectino_name.getIndexes()
Output-->
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "some_info"
},
{
"v" : 2,
"unique" : true,
"key" : {
"id" : 1
},
"name" : "__primary_key__",
"ns" : "some_info"
},
{
"v" : 2,
"key" : {
"field1" : 1
},
"name" : "some_info_field1_a59e0e95",
"ns" : "some_info"
}
]
这样索引就创建好了。伟大的。 现在我想删除索引。我将我的 model.py 更改为如下所示。
class ABC(models.Model):
field1 = models.CharField(max_length=255)
field2 = models.CharField(max_length=255)
然后成功运行 makemigration,但在迁移时出错。
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command
_line
utility.execute()
File "venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "venv/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "venv/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "venv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "venv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "venv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "venv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forw
ards
schema_editor.alter_field(from_model, from_field, to_field)
File "venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 523, in alter_field
old_db_params, new_db_params, strict)
File "venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 586, in _alter_field
index_names = self._constraint_names(model, [old_field.column], index=True, type_=Index.suffix)
File "venv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 1047, in _constraint_names
if type_ is not None and infodict['type'] != type_:
KeyError: 'type'
无法执行迁移:(。在这里我可以获取 type_(ie idx) 值但不能获取 infodict['type']
但是如果我从 mongoshell 中删除索引(使用 dropIndex),那么我可以运行迁移。
那么知道下面有什么问题吗?
这是requirements.txt
pymongo==3.7.2
djongo==1.2.31
Django==2.1.7
coreschema==0.0.4
sqlparse==0.2.4
我也检查了这些链接。