无服务器定义具有 GlobalSecondaryIndexes 的 Dynamodb

Serverless define a Dynamodb that has GlobalSecondaryIndexes

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

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

    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",
..........
}

您似乎定义了一个名为 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}