在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
相关产品推荐
相关产品推荐

