Home > Enterprise >  The number of attributes in key schema must match the number of attributes defined in attribute defi
The number of attributes in key schema must match the number of attributes defined in attribute defi

Time:01-23

While creating table in dynamoDB from Node Js. I got the above error.

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" },
        { AttributeName: "controller_type", AttributeType: "S" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    }
}

{ AttributeName: "controller_type", AttributeType: "S" }

adding this attribute to AttributeDefinitions I'm getting this above error

CodePudding user response:

As DynamoDB is schemaless, you need only provide attributes which are needed as partition/sort key at the time of creation, this is why the parameter is named KeySchema as its only related to the keys:

Array Members: Minimum number of 1 item. Maximum number of 2 items.

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
    }
}

CodePudding user response:

The number of attributes in the key schema of a table in Amazon DynamoDB must match the number of attributes defined in the attribute definitions.

In DynamoDB, a key schema is used to define the primary key of a table. The primary key consists of one or more attributes, and each attribute is defined in the attribute definitions. The key schema must include the exact same set of attributes as those defined in the attribute definitions, and in the same order. This ensures that there is a one-to-one mapping between the attributes in the key schema and the attributes defined in the attribute definitions.

For example, if the primary key of a table is defined as a composite key consisting of two attributes, "Id" and "Timestamp", then the attribute definitions must include these two attributes, and the key schema must include these two attributes in the same order.

It's important to note that the primary key of a DynamoDB table must be unique and cannot be null.

  • Related