微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

创建具有键架构上不存在的属性的表时出现问题

如何解决创建具有键架构上不存在的属性的表时出现问题

我正在学习 DynamoDB。

我想用以下主键创建一个表:

  • 分区键:名称
  • 排序键:creationDate

并具有以下属性

  • 姓名 (pk)
  • 地址(sk)
  • 创建日期
  • isActive

这是我的 TS 界面(以防万一它有助于描述我想要的)

interface User {
  readonly name: string;
  readonly address: string;
  readonly creationDate: Date;
  readonly isActive: boolean;
}

我编写了以下模板:

Resources:
  myDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDeFinitions:
      - AttributeName: 'name'
        AttributeType: 'S'
      - AttributeName: 'address'
        AttributeType: 'S'
      - AttributeName: 'creationDate'
        AttributeType: 'S'
      - AttributeName: 'isActive'
        AttributeType: 'S'
      KeySchema:
      - AttributeName: 'name'
        KeyType: HASH
      - AttributeName: 'creationDate'
        KeyType: RANGE

当我部署它时,它失败并显示以下错误

One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDeFinitions

我用谷歌搜索并找到了几个答案,但我仍然不明白发生了什么以及如何在我的表中包含这些属性(所有项目都将具有这些属性)。

有什么办法可以做到吗?如果是这样,我做错了什么? 谢谢

解决方法

您的 isActive 中不能有 addressAttributeDefinitions,因为它们不在您的 KeySchema 中。您还缺少 ProvisionedThroughput。所以正确的表定义是(示例):

Resources:
  myDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
      - AttributeName: 'name'
        AttributeType: 'S'
      - AttributeName: 'creationDate'
        AttributeType: 'S'
      KeySchema:
      - AttributeName: 'name'
        KeyType: HASH
      - AttributeName: 'creationDate'
        KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。