python - Azure CosmosDB 为什么在插入 Azure CosmosDB 时不自动添加 'id' 字段?

标签 python python-3.x visual-studio-code azure-functions azure-cosmosdb

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

该函数应用程序对我的同事来说运行得非常好,他们没有收到此错误。 每当我尝试访问 azure cosmos db 时都会发生错误。

示例

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

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

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

我正在使用 Visual Studio Code,用 Python 编码,安装了 azure 工具扩展。我尝试从磁盘中删除代码并重新克隆它,删除所有扩展并重新安装它们,我什至完全卸载了 Visual Studio Code。无论设置发生什么变化,它仍然存在。

编辑

添加了容器属性

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/'}

最佳答案

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

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

Azure-Cosmos-4.3.0 github link to the line

令人烦恼的是,这一行是硬编码的。为什么他们决定阻止用户自动生成 id s 超出了我的范围。

关于python - Azure CosmosDB 为什么在插入 Azure CosmosDB 时不自动添加 'id' 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72535715/

相关文章:

python - T.hessian 在 theano 中给出 NotImplementedError()

Python 对 CSV 进行排序和求和

python - ndb 有没有 list 属性

python - 在列表理解中理解这个 if 语句

typescript - 代码格式化 Visual Studio Code - 行长

visual-studio-code - VS 代码中的扩展警告

python - 我怎样才能从 mongodb 中获取所有具有相同值的数据?

python - 如何在 Pandas 中进行 Groupby 比率计算

Python 字符串连接内部细节

visual-studio-code - 如何使用 editor.tokenColorCustomizations 而不是工作台更改 vscode 中的编辑器背景颜色