Mongoengine create

编程入门 行业动态 更新时间:2024-10-25 08:23:55
Mongoengine create_user函数生成null objectId(Mongoengine create_user function generating null objectId's)

在将单个用户添加到数据库后,似乎总是得到完全相同的错误。

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()

更多推荐

本文发布于:2023-07-15 05:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mongoengine   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!