简体   繁体   English

如何在不指定 gsi 键的情况下创建 dynamo db 项目

[英]How to create dynamo db item without specifying gsi key

I am working with a dynamo db table that uses a gsi so I can query via an additional attribute, if it is present.我正在使用使用 gsi 的 dynamo db 表,因此我可以通过附加属性进行查询(如果存在)。 This attribute will be updated in a process so all of the items do not have this attribute in the beginning of the process.此属性将在流程中更新,因此所有项目在流程开始时都没有此属性。 This attribute is set as the primary key of the gsi, when added to the item.此属性在添加到项目时设置为 gsi 的主键。

Everything works as intended besides the fact that I cannot insert a new item without also specifying the primary key of the gsi.除了指定 gsi 的主键我无法插入新项目之外,一切都按预期工作。 I want the gsi to ignore the item if its primary key is not set yet.如果尚未设置其主键,我希望 gsi 忽略该项目。 I thought this would be possible with the option INCLUDE, when specifying the projected attributes.在指定投影属性时,我认为使用 INCLUDE 选项可以做到这一点。 Obviously I am getting it wrong here but I also don't know how to solve this problem.显然我在这里弄错了,但我也不知道如何解决这个问题。

Any help is much appreciated and also I will have to stick to dynamo db so every hint including this db helps a lot!非常感谢任何帮助,而且我将不得不坚持使用 dynamo db,所以包括这个 db 在内的每一个提示都会有很大帮助!

EDIT: For clarification - these are my attributes:编辑:为了澄清 - 这些是我的属性:

  • id (Primary Key: mandatory) id(主键:必填)
  • name (Attribute)名称(属性)
  • year (Attribute)年份(属性)
  • gsi_id (GSI Primary Key: optional) gsi_id(GSI 主键:可选)

I want to add an item with the fields:我想添加一个带有以下字段的项目:

  • id (Primary Key: mandatory) id(主键:必填)
  • name (Attribute)名称(属性)
  • year (Attribute)年份(属性)

and later add the gsi_id field.然后添加 gsi_id 字段。

The premise of your question is that你的问题的前提是

I cannot insert a new item without also specifying the primary key of the GSI.如果不指定 GSI 的主键,我就无法插入新项目。

However, I believe this premise is incorrect.但是,我认为这个前提是不正确的。 According to both my experience and the documentation, you are very much allowed to insert an item without the GSI key attribute to the base table, and this item will be added to the base table but be missing in the index - exactly what you want to happen.根据我的经验和文档,非常允许您将没有GSI 键属性的项目插入到基表中,并且该项目将被添加到基表中但在索引中丢失 - 正是您想要的发生。

Here is for example a snippet from the documentation :例如,这是文档中的一个片段:

A global secondary index only tracks data items where its key attributes actually exist.全局二级索引仅跟踪其关键属性实际存在的数据项。 For example, suppose that you added another new item to the GameScores table, but only provided the required primary key attributes.例如,假设您向 GameScores 表添加了另一个新项目,但只提供了所需的主键属性。 ... Because you didn't specify the TopScore attribute, DynamoDB would not propagate this item to GameTitleIndex. ... 因为您没有指定 TopScore 属性,DynamoDB 不会将此项目传播到 GameTitleIndex。

Note that this is only true for a missing attribute.请注意,这仅适用于缺少的属性。 Something you really are not allowed to do is to set a value with the wrong type in the GSI key attribute.您真正不允许做的事情是在 GSI 键属性中设置错误类型的值。 For example, if the GSI key attribute is defined to have "number" type, you cannot set a string in that attribute - you'll get a ValidationException error on the update operation.例如,如果 GSI 键属性被定义为“数字”类型,则您不能在该属性中设置字符串 - 您将在更新操作时收到ValidationException错误。 But having the attribute completely missing is fine.但是完全缺少该属性是可以的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM