Search code examples
node.jsamazon-web-servicesamazon-dynamodbcreate-table

Nodejs DynamoDB CreateTable hash only with no range returns ValidationException


I am trying to create a table on dynamodb trough their aws-sdk on nodejs. Below is the parameter I am passing on dynamodb.createTable:

{
    TableName: 'new_table',
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    },
    KeySchema: [
        {AttributeName: 'primary_key', KeyType: 'HASH'}
    ],
    AttributeDefinitions: [
        {AttributeName: 'primary_key', AttributeType: 'S'},
        {AttributeName: 'some_attribute', AttributeType: 'S'}
    ]
}

This returns

ValidationException: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions

I have fixed this by adding 'some_attribute' on 'KeySchema' but what I want is a 'hash' only table with no 'range'.


Solution

  • Solved: When creating a table, you only need to specify the KeySchema attributes and not all attributes that you will be putting on the table. So in my case I just need to specify the 'primary_key'

    {
        TableName: 'new_table',
        ProvisionedThroughput: {
            ReadCapacityUnits: 1,
            WriteCapacityUnits: 1
        },
        KeySchema: [
            {AttributeName: 'primary_key', KeyType: 'HASH'}
        ],
        AttributeDefinitions: [
            {AttributeName: 'primary_key', AttributeType: 'S'},
        ]
    }