首页 > 解决方案 > 在 DynamoDB 中查询索引导致主键错误

问题描述

我发现很难在表上查询 GSI。尽管我正在使用索引,但下面的代码示例会导致与主键相关的键错误。

表定义:

TableName : config.user_table,
KeySchema: [       
    { AttributeName: "user", KeyType: "HASH"}
],
AttributeDefinitions: [       
    { AttributeName: "user", AttributeType: "S" },
    { AttributeName: "activation_code", AttributeType: "S" },
],
GlobalSecondaryIndexes: [
    {
        IndexName: "activation_code_index",
        KeySchema: [
            {AttributeName: "activation_code", KeyType: "HASH"}
        ],
        Projection: {
            ProjectionType: "ALL"
        },
        ProvisionedThroughput: {
            "ReadCapacityUnits": 1,"WriteCapacityUnits": 1
        }
    }
],
ProvisionedThroughput: {       
    ReadCapacityUnits: 10, 
    WriteCapacityUnits: 10
}

查询定义:

var params = {
    TableName: config.user_table,
    IndexName: "activation_code_index",
    KeyConditionExpression: "activation_code = :code", 
    ExpressionAttributeValues: {
        ":code": code
    }
};
return dcl.query(params).promise();

错误:

message: 'One or more parameter values were invalid: Missing the key user in the item',
code: 'ValidationException',

标签: amazon-dynamodb

解决方案


推荐阅读