Number of attributes in key schema must match the number of attributes defined in attribute definitions

前端 未结 3 820
囚心锁ツ
囚心锁ツ 2021-01-30 19:03

I’m trying to create a simple table using DynamoDB JavaScript shell and I’m getting this exception:

{
  "message&quo         


        
3条回答
  •  情歌与酒
    2021-01-30 19:52

    When you use non-key attribute in at "AttributeDefinitions", you must use it as index, otherwise it's against the way of DynamoDB to work. See the link.

    So no need to put a non-key attribute in "AttributeDefinitions" if you're not gonna use it as index or primary key.

    var params = {
            TableName: 'table_name',
            KeySchema: [ // The type of of schema.  Must start with a HASH type, with an optional second RANGE.
                { // Required HASH type attribute
                    AttributeName: 'UserId',
                    KeyType: 'HASH',
                },
                { // Optional RANGE key type for HASH + RANGE tables
                    AttributeName: 'RemindTime', 
                    KeyType: 'RANGE', 
                }
            ],
            AttributeDefinitions: [ // The names and types of all primary and index key attributes only
                {
                    AttributeName: 'UserId',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                {
                    AttributeName: 'RemindTime',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                {
                    AttributeName: 'AlarmId',
                    AttributeType: 'S', // (S | N | B) for string, number, binary
                },
                // ... more attributes ...
            ],
            ProvisionedThroughput: { // required provisioned throughput for the table
                ReadCapacityUnits: 1, 
                WriteCapacityUnits: 1, 
            },
            LocalSecondaryIndexes: [ // optional (list of LocalSecondaryIndex)
                { 
                    IndexName: 'index_UserId_AlarmId',
                    KeySchema: [ 
                        { // Required HASH type attribute - must match the table's HASH key attribute name
                            AttributeName: 'UserId',
                            KeyType: 'HASH',
                        },
                        { // alternate RANGE key attribute for the secondary index
                            AttributeName: 'AlarmId', 
                            KeyType: 'RANGE', 
                        }
                    ],
                    Projection: { // required
                        ProjectionType: 'ALL', // (ALL | KEYS_ONLY | INCLUDE)
                    },
                },
                // ... more local secondary indexes ...
            ],
        };
        dynamodb.createTable(params, function(err, data) {
            if (err) ppJson(err); // an error occurred
            else ppJson(data); // successful response
        });
    

提交回复
热议问题