【问题标题】:DynamoDb PutItem seems correct, but ValidationException: The provided key element does not match the schemaDynamoDb PutItem 似乎是正确的,但 ValidationException: The provided key element does not match the schema
【发布时间】:2021-08-13 14:53:19
【问题描述】:

我知道之前有人问过这个问题,并且根据这些答案,我很确定我所拥有的是正确的。在这一点上,老实说,我觉得我错过了一些超级基本的东西。

DynamoDB 连接器已设置:

const AWS = require('aws-sdk');

// set up dynamoDB
const dynamoDb = new AWS.DynamoDB.DocumentClient();

我已经在无服务器中定义了一个带有键的 DynamoDB 表:

CampaignStreamDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
        AttributeDefinitions:
          - AttributeName: "id"
            AttributeType: "S"
          - AttributeName: "campaign_id"
            AttributeType: "S"
        KeySchema:
          - AttributeName: "id"
            KeyType: "HASH"
          - AttributeName: "campaign_id"
            KeyType: "RANGE"
        StreamSpecification:
          StreamViewType: "NEW_AND_OLD_IMAGES"
        TableName: ${self:provider.environment.FS_CAMPAIGN_STREAM_TABLE}

我有一个函数可以更新包含主键和范围键的表(第三个参数是我更新测试的状态):

let update_test_status = async (test_id, campaign_key, test_status) => {

console.info("Updating bulk test " + test_id + " with campaign_key "+campaign_key+" to " + test_status);
    let b = true;
    const timestamp = new Date().getTime();

    const update_params = {
        TableName: process.env.FS_CAMPAIGN_STREAM_TABLE,
        Key: {
            id: test_id,
            campaign_key: campaign_key
        },
        ProjectionExpression: "#s",
        ExpressionAttributeNames: {"#s": "status"},
        UpdateExpression: "set #s=:s, updated_at=:u",
        ExpressionAttributeValues: {
            ":s": test_status,
            ":u": timestamp,
        },
        ReturnValues: "UPDATED_NEW"
    };

    try {
        let results = await dynamoDb.update(update_params).promise();

    } catch (error) {
        console.error("Could not update the record: " + error);
        test_status = "error";
        console.error("Links processed, but error updating record in DB");
        b = false;

    }
    return b;

};
    
        try {
            let results = await dynamoDb.update(update_params).promise();
    
        } catch (error) {
            console.error("Could not update the record: " + error);
            test_status = "error";
            console.error("Links processed, but error updating record in DB");
            b = false;
    
        }
        return b;
    };

我这样称呼这个函数:

await update_test_status(item.id.S, item.campaign_key.S, "processing")

其中 item 是 DynamoDB 记录

注意事项:

我已经验证了 id 和 key 都被传递给了 update 方法。 我试过带引号和不带引号的 KEY 参数

【问题讨论】:

    标签: amazon-dynamodb


    【解决方案1】:

    在您的表定义中,您将其称为 campaign_id,但稍后在您的代码中,您将其称为 campaign_key。

    当看到错误时,您应该始终双重(和三重)检查哈希和范围键的名称和类型是否与定义中的相同。

    【讨论】:

    • @picus - 不 - 我也花了 10 分钟试图了解错误的其他来源。就在我接受它必须是由于命名或类型不匹配时,我又读了你的问题 5 次以注意到那个错字。下次我看到错误时,我知道,这是由于命名或类型不匹配 :)
    猜你喜欢
    • 2023-02-24
    • 2016-08-23
    • 2022-12-27
    • 1970-01-01
    • 2013-05-14
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多