在将单个用户添加到数据库后,似乎总是得到完全相同的错误。
Tried to save duplicate unique keys (E11000 duplicate key error index: mydb.user.$objectId_1 dup key: { : null })奇怪的是,我从测试套件中完全复制了代码,即使这样也行不通。 我查看堆栈跟踪,并且有问题的问题总是在同一个地方
/usr/local/lib/python2.7/dist-packages/mongoengine/django/auth.py in set_password 264. self.save()令人难以置信的是令人沮丧的是,我基本上在过去的两天里一直在关注这个问题。 这不是我的代码中的东西。 由于某种原因,它似乎没有生成ObjectId,因为它始终为null。 我甚至不明白为什么会这样
我的代码很简单
from mongoengine.django.auth import User from django.contrib.auth import get_user_model user_data = { 'username': 'user', 'email': 'user@example.com', 'password': 'test', } manager = get_user_model()._default_manager user = manager.create_user(user_data)It always seems to get the exact same error after I've added a single user to the DB.
Tried to save duplicate unique keys (E11000 duplicate key error index: mydb.user.$objectId_1 dup key: { : null })What's strange is that I copied the code EXACTLY from the test suite and even that doesn't work. I look at the stack trace and the offending issue is always in the same place
/usr/local/lib/python2.7/dist-packages/mongoengine/django/auth.py in set_password 264. self.save()It's incredibly frustrating and I have been looking at this for basically the last 2 days. It's not something in my code. For some reason it doesn't seem to be generating an ObjectId as it's always null. I don't even understand why that's the case
My code is simply
from mongoengine.django.auth import User from django.contrib.auth import get_user_model user_data = { 'username': 'user', 'email': 'user@example.com', 'password': 'test', } manager = get_user_model()._default_manager user = manager.create_user(user_data)最满意答案
看起来你在objectId字段上有一个额外的索引是唯一的
创建新用户时,它不设置objectId字段值,并且作为其唯一,您不能有两个没有值的文档(因为它不是稀疏索引)。
要在mongo shell中测试:
> use mydb > db.user.getIndexes()删除objectId_1索引:
> db.user.dropIndex("objectId_1") > db.user.getIndexes()Looks like you have an extra index on an objectId field that is Unique
When creating a new User its not setting an objectId field value and as its unique you can't have two documents without a value (as its not a sparse index).
To test in the mongo shell:
> use mydb > db.user.getIndexes()Drop the objectId_1 index:
> db.user.dropIndex("objectId_1") > db.user.getIndexes()更多推荐
发布评论