The number of attributes in the key scheme must match the number of attributes defined in the attribute definitions

I am trying to create a simple table using the DynamoDB javascript shell and I get this exception:

{ "message": "The number of attributes in key schema must match the number of attributes defined in attribute definitions.", "code": "ValidationException", "time": "2015-06-16T10:24:23.319Z", "statusCode": 400, "retryable": false } 

The following is an Im table trying to create:

 var params = { TableName: 'table_name', KeySchema: [ { AttributeName: 'hash_key_attribute_name', KeyType: 'HASH', }, ], AttributeDefinitions: [ { AttributeName: 'hash_key_attribute_name', AttributeType: 'S', }, { AttributeName: 'attribute_name_1', AttributeType: 'S', } ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }; dynamodb.createTable(params, function(err, data) { if (err) print(err); else print(data); });
var params = { TableName: 'table_name', KeySchema: [ { AttributeName: 'hash_key_attribute_name', KeyType: 'HASH', }, ], AttributeDefinitions: [ { AttributeName: 'hash_key_attribute_name', AttributeType: 'S', }, { AttributeName: 'attribute_name_1', AttributeType: 'S', } ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }; dynamodb.createTable(params, function(err, data) { if (err) print(err); else print(data); }); 

However, if I add a second attribute to keySchema, it works fine. Below the desktop:

 var params = { TableName: 'table_name', KeySchema: [ { AttributeName: 'hash_key_attribute_name', KeyType: 'HASH', }, { AttributeName: 'attribute_name_1', KeyType: 'RANGE', } ], AttributeDefinitions: [ { AttributeName: 'hash_key_attribute_name', AttributeType: 'S', }, { AttributeName: 'attribute_name_1', AttributeType: 'S', } ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }; dynamodb.createTable(params, function(err, data) { if (err) print(err); else print(data); });
var params = { TableName: 'table_name', KeySchema: [ { AttributeName: 'hash_key_attribute_name', KeyType: 'HASH', }, { AttributeName: 'attribute_name_1', KeyType: 'RANGE', } ], AttributeDefinitions: [ { AttributeName: 'hash_key_attribute_name', AttributeType: 'S', }, { AttributeName: 'attribute_name_1', AttributeType: 'S', } ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }; dynamodb.createTable(params, function(err, data) { if (err) print(err); else print(data); }); 

I do not want to add a range to the key scheme. Any idea how to fix this?

+35
amazon-dynamodb dynamo-local
Jun 16 '15 at 11:15
source share
3 answers

DynamoDB is schematic (except key scheme)

That is, you need to specify the key scheme (name and type of attribute) when creating the table. Well, you do not need to specify any non-key attributes. You can put an element with any attribute later (you must include the keys, of course).

On the documentation page, AttributeDefinitions is defined as:

An array of attributes that describe the key scheme for the table and indexes.

When creating a table, the AttributeDefinitions field is used only for hashes and / or range ranges. In your first case, there is only a hash key (number 1), while you provide 2 attributes. This is the main reason for the exception.

TL; DR Do not include fuzzy attribute definitions in AttributeDefinitions .

+89
Jun 18 '15 at 19:44
source share

When you use a non-key attribute in the "AttributeDefinitions" section, you must use it as an index, otherwise it will work against dynamodb. see link

Thus, you do not need to put a non-key attribute in "AttributeDefinitions" unless you intend to use it as an 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 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 }); 
+3
Aug 29 '16 at 13:30
source share

I also had this problem and I will post here what went wrong for me if it helps someone else.

In my CreateTableRequest, I had an empty array for GlobalSecondaryIndexes.

 CreateTableRequest createTableRequest = new CreateTableRequest { TableName = TableName, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 2, WriteCapacityUnits = 2 }, KeySchema = new List<KeySchemaElement> { new KeySchemaElement { AttributeName = "Field1", KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = "Field2", KeyType = KeyType.RANGE } }, AttributeDefinitions = new List<AttributeDefinition>() { new AttributeDefinition { AttributeName = "Field1", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "Field2", AttributeType = ScalarAttributeType.S } }, //GlobalSecondaryIndexes = new List<GlobalSecondaryIndex> //{ //} }; 

Commenting on these lines in creating the table, I solved my problem. Therefore, I think the list should be empty, not empty.

0
May 26 '16 at 13:25
source share



All Articles