django-nonrel syncdb和mongodb:pymongo.errors.OperationFailure

时间:2016-12-12 04:01:46

标签: python mongodb django-nonrel

我正在使用django-nonrel和mongodb-engine当我运行python manage.py syncdb时出现以下错误:

Traceback (most recent call last):
File "manage.py", line 22, in <module> execute_from_command_line(sys.argv)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 147, in handle_noargs
index_sql = connection.creation.sql_indexes_for_model(model, self.style)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 49, in sql_indexes_for_model
self._handle_oldstyle_indexes(ensure_index, meta)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 116, in _handle_oldstyle_indexes
sparse=field.name in sparse_indexes)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/django_mongodb_engine/creation.py", line 42, in ensure_index
return collection.ensure_index(*args, **kwargs)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/collection.py", line 1420, in ensure_index
self.__create_index(keys, kwargs)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/collection.py", line 1298, in __create_index
sock_info, cmd, read_preference=ReadPreference.PRIMARY)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/collection.py", line 208, in _command
read_concern=read_concern)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/pool.py", line 239, in command
read_concern)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/network.py", line 102, in command
helpers._check_command_response(response_doc, None, allowable_errors)
File "/Users/<user>/site/qmcdb/lib/python2.7/site-packages/pymongo/helpers.py", line 205, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: The field 'sparse' is not valid for an _id index specification. Specification: { ns: "qmcdb_mongodb.django_admin_log", v: 2, sparse: false, unique: true, name: "_id_1", key: { _id: 1 } }

我不知道出了什么问题。我一直试图让mongodb与django合作,我遇到了很多问题。我的settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine',
        'NAME': 'qmcdb_mongodb',
        'PORT': 27017,
        'HOST': 'localhost'
    }
}

我希望我已经发布了足够的背景信息。我真的很感激任何帮助。

4 个答案:

答案 0 :(得分:4)

在版本3.2中尝试使用Mongo数据库,mongoengine和Mongo Server 3.4存在一些问题

答案 1 :(得分:2)

1.MongoDB 3.2版3.4中的测试syncdb,它仍然会引发错误。

2.在MongoDB 2.6中,syncdb运行正常,但有一个小小的无关问题。我在版本2.6中仍然出现错误并使用此修复(https://gist.github.com/ielshareef/2986459)。

3.django_mongodb_engine是如此的错误,它是从django1.3分叉出来的。此外,这个回购已经过时,最后一次提交是在2015年7月13日。我不应该推荐这个包。

4.我会推荐https://github.com/MongoEngine/django-mongoengine

答案 2 :(得分:0)

我的hacky解决方案是修改Lib\site-packages\django_mongodb_engine\creation.py的第115-116行 从这个:

ensure_index(column, unique=field.unique,
                     sparse=field.name in sparse_indexes)

到此:

ensure_index(column)

似乎可以清除错误,但我确信它会让某些地方失败,因为我不知道自己在做什么。

我也会等待更好的答案。

答案 3 :(得分:0)

我建议您使用:

pip3 install git+https://github.com/MongoEngine/django-mongoengine
Django 1.11.2
Python 3.6
MongoDB 3.4

总是很好,使用更新包的版本,使用环境隔离。 https://virtualenvwrapper.readthedocs.io/en/latest/install.html