You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Node.js创建DynamoDB表时遇Key Schema与AttributeDefinitions属性数不匹配错误

解决DynamoDB创建表时的键架构与属性定义不匹配错误

这个错误的核心原因是DynamoDB的硬性规则:表的AttributeDefinitions只能包含KeySchema中指定的主键属性,不能添加非主键的额外属性(比如你加入的controller_type)。

DynamoDB属于无模式数据库,非主键字段不需要在创建表阶段提前定义,后续写入数据时直接添加即可。

修正后的表定义代码如下:

const schema = {
    TableName: "alarms",
    KeySchema: [
        { AttributeName: "alarm_code", KeyType: "HASH" },  //Partition key
        { AttributeName: "controller", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "alarm_code", AttributeType: "N" },
        { AttributeName: "controller", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
}

如果后续需要基于controller_type字段创建索引,可在创建索引的配置中添加对应的属性定义,无需在表的基础结构里声明。

内容的提问来源于stack exchange,提问作者Ashik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 02:55:15