【问题标题】:Serverless define a Dynamodb that has GlobalSecondaryIndexes无服务器定义具有 GlobalSecondaryIndexes 的 Dynamodb
【发布时间】:2021-03-03 20:42:38
【问题描述】:

无服务器错误 ---------------------------------------- 我已经返回并转发了文档。似乎这应该是一件简单的事情,但无法弄清楚为什么它不让我

发生错误:documentsTable - 一个或多个参数值无效:某些索引键属性未在 AttributeDefinitions 中定义。键:[UserId],AttributeDefinitions:[userId, id](服务:AmazonDynamoDBv2;状态代码:400;错误代码:ValidationException;请求 ID:K28DP2ST778DAN7CP1MV4BDKM3VV4KQNSO5AEMVJF66Q9ASUAAJG;代理:null)。

    documentsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: userId
            AttributeType: S
        KeySchema:
          - AttributeName: UserId
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: userIdIndex
            KeySchema:
              - AttributeName: UserId
                KeyType: HASH
            Projection:
              ProjectionType: ALL
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:custom.env}-${self:custom.prefix}-${self:custom.documentDynamoTable2}

那么我当然会有这样的查询

query(
            {
                TableName: documentTable,
                IndexName: 'userIdIndex',
                KeyConditionExpression: '#userId = :userId',
                ExpressionAttributeValues: {
                    ':userId': aUserId
                },
                ExpressionAttributeNames: {
                    "#userId": "userId"
                },
                Select: 'ALL_ATTRIBUTES',
            }
        ).promise();

错误:

{
    "errorType": "ValidationException",
    "errorMessage": "Query condition missed key schema element: id",
    "code": "ValidationException",
    "message": "Query condition missed key schema element: id",
    "time": "2021-03-03T22:59:22.298Z",
    "requestId": "24B4KVLCT9PTIDPPN06ITUTG0JVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "statusCode": 400,
    "retryable": false,
    "retryDelay": 28.89477589112033,
    "stack": [
        "ValidationException: Query condition missed key schema element: id",
..........
}

【问题讨论】:

    标签: amazon-dynamodb serverless-framework serverless dynamodb-queries


    【解决方案1】:

    看起来您正在定义一个名为 userId(小写 u)的属性,后来又引用了 UserId(大写 U)。

    你想要这个

    documentsTable:
          Type: AWS::DynamoDB::Table
          Properties:
            AttributeDefinitions:
              - AttributeName: id
                AttributeType: S
              - AttributeName: userId
                AttributeType: S
            KeySchema:
              - AttributeName: userId      // <--- CHANGE HERE
                KeyType: HASH 
            GlobalSecondaryIndexes:
              - IndexName: userIdIndex
                KeySchema:
                  - AttributeName: userId  // <--- CHANGE HERE
                    KeyType: HASH
                Projection:
                  ProjectionType: ALL
                ProvisionedThroughput:
                  ReadCapacityUnits: 1
                  WriteCapacityUnits: 1
            ProvisionedThroughput:
              ReadCapacityUnits: 1
              WriteCapacityUnits: 1
            TableName: ${self:custom.env}-${self:custom.prefix}-${self:custom.documentDynamoTable2}
    

    【讨论】:

    • 谢谢@Seth 你在哪里,但现在如果我做对了,运行查询并收到我需要传递 ID 的错误,但我想通过 userId 获取所有对象。
    • 为什么要创建一个与基表具有相同 PK 的 GSI?
    • @Kirk 提出了一个很好的观点,这让我想知道是否对 DynamoDB 索引存在一些混淆。 DynamoDB 索引与它们的 RDBMS 对应物不相同。无需在 DynamoDB 中创建与基表中的主键匹配的二级索引,这是多余的。 DDB 中的索引旨在支持其他访问模式。例如,您的基表设置为按 ID 获取用户。如果您想通过其他属性(例如电子邮件)获取用户,您可以创建一个索引并将 PK 设置为电子邮件。
    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 2019-06-30
    • 2014-03-19
    • 1970-01-01
    • 2011-05-22
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多