gpt4 book ai didi

Azure CosmosDB why isn't the 'id' field automatically added when inserting into Azure CosmosDB?(Azure CosmosDB为什么在插入Azure CosmosDB时不自动添加“id”字段?)

转载 作者:bug小助手 更新时间:2023-10-22 13:03:34 35 4
gpt4 key购买 nike



I have recently started getting this error when I debug my python function app: (BadRequest) Message: {"Errors":["One of the specified inputs is invalid"]}

我最近在调试python函数应用程序时开始出现此错误:(BadRequest)消息:{“错误”:[“指定的输入之一无效”]}


The function app works perfectly well for my coworkers and they are not getting this error.
The error occurs whenever I attempt to access the azure cosmos db.

该功能应用程序非常适合我的同事,他们没有收到这个错误。每当我试图访问azure cosmos数据库时,就会出现错误。


Sample

样品


item = { "firstname": "John", "lastname": "Doe"}
container.upsert_item(item)

The issue seems to be that insert into the database is missing the "id" field. If I add this field to the item before calling upsert_item(item) it works correctly.

问题似乎是插入到数据库中缺少“id”字段。如果我在调用upsert_item(item)之前将此字段添加到项目中,它将正常工作。


Why did I not originally need to do this (coworkers don't need to) and what setting did I change to stop it from auto adding the id field?

为什么我最初不需要这样做(同事不需要),我更改了什么设置来阻止它自动添加id字段?


I am using visual studio code, coding in python, installed the azure tools extension. I have tried deleting the code from disk and re-cloning it, removing all the extensions and reinstalling them, and I even completely uninstalled visual studio code. Whatever setting changed is still there.

我使用的是visualstudio代码,用python编码,安装了azure工具扩展。我试着从磁盘上删除代码并重新克隆,删除所有扩展并重新安装,我甚至完全卸载了visualstudio代码。无论环境发生了什么变化,都仍然存在。


EDIT

编辑


Added the container properties

添加了容器属性


Container properties {'id': 'person', 'indexingPolicy': {'indexingMode': 'consistent', 'automatic': True, 'includedPaths': [{'path': '/*'}], 'excludedPaths': [{'path': '/"_etag"/?'}]}, 'partitionKey': {'paths': ['/lastname'], 'kind': 'Hash'}, 'uniqueKeyPolicy': {'uniqueKeys': []}, 'conflictResolutionPolicy': {'mode': 'LastWriterWins', 'conflictResolutionPath': '/_ts', 'conflictResolutionProcedure': ''}, 'geospatialConfig': {'type': 'Geography'}, 'analyticalStorageTtl': -1, '_rid': 'O-pDAPQSY5Y=', '_ts': 1654191756, '_self': 'dbs/O-pDAA==/colls/O-pDAPQSY5Y=/', '_etag': '"00003b31-0000-0100-0000-6298f68c0000"', '_docs': 'docs/', '_sprocs': 'sprocs/', '_triggers': 'triggers/', '_udfs': 'udfs/', '_conflicts': 'conflicts/'}

更多回答

Remember that an upsert is going to check against existing documents, for matching id (to decide whether to insert or update), making id required (vs a call to create_item() which would generate a default id if you didn't provide it).

请记住,upstart将根据现有文档检查是否匹配id(以决定是否插入或更新),是否需要id(与调用create_item()相比,如果您没有提供默认id,它将生成默认id)。

@DavidMakogon - I changed the code to use create_item(item) and I instead get (BadRequest) Message: {"Errors":["The input content is invalid because the required properties - 'id; ' - are missing. Learn more: https:\/\/aka.ms\/CosmosDB\/sql\/errors\/missing-id"]} So some setting changed somewhere that now requires me to input an id when I didn't originally have to

@DavidMakogon-我将代码更改为使用create_item(item),而得到(BadRequest)消息:{“Errors”:[“输入内容无效,因为缺少所需的属性-“id;”。了解更多信息:https:\/\/aka.ms\/CommosDB\/sql\/Errors\/missing id”]}因此,某些设置发生了更改,现在要求我在原来不需要输入id的情况下输入id

Does this answer your question? Have CosmosDB choose the ID of the inserted item

这能回答你的问题吗?让CosmosDB选择插入项目的ID

优秀答案推荐

Found the answer to my issue.
I had updated azure cosmos db to the latest version (4.3.0) globaly (which is why it only affected me, should have put it in the requirements.txt instead) which does not allow for the automatic creation of ids.

找到了我问题的答案。我已经将azure cosmos db更新到了globaly的最新版本(4.3.0)(这就是为什么它只影响我,应该把它放在requirements.txt中),它不允许自动创建id。


There is a single line change (from 4.2.0 to 4.3.0) in the upsert_item(...) function that is difficult to spot (worded very similarly) when you are looking at a wall of code.

upsert_item(…)函数中有一行更改(从4.2.0更改为4.3.0),当您查看代码墙时,很难发现(措辞非常相似)。


Azure-Cosmos-4.3.0 github link to the line

Azure-Cosmos-4.3.0 github连接到线路


Annoyingly this line is hard coded. Why they decided to prevent the user from auto generating ids is beyond me.

令人恼火的是,这行代码很硬。我无法理解他们为什么决定阻止用户自动生成id。



If people are still looking for this:
You can now add the enable_automatic_id_generation flag to the create_item method.

如果人们仍在寻找:现在可以将enable_automatic_id_generation标志添加到create_item方法中。


Example:

示例:


result = container.create_item(
{
"data": "hello world",
},
enable_automatic_id_generation=True,
)

更多回答

You can easily generate the id on the client side if you need to by using the UUID generators. Having said that, when you generate random IDs, it would be hard for you to track them and then it will be difficult to perform point reads where you would need Partition Key and Item ID.

如果需要,可以使用UUID生成器在客户端轻松生成id。话虽如此,当你生成随机ID时,你很难跟踪它们,然后很难在需要分区密钥和项目ID的地方执行点读取。

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com