user2924127
user2924127

Reputation: 6242

create a table with a global secondary index error

I am trying to create a table with a global secondary index using the javascript SDK inside of nodejs:

var messagesTableParams = {
    TableName : "Messages",
    KeySchema: [
        { AttributeName: "to", KeyType: "HASH"},  //Partition key
        { AttributeName: "tm", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [
        { AttributeName: "to", AttributeType: "N" },
        { AttributeName: "tm", AttributeType: "N" }
    ],
    ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10
    },
    GlobalSecondaryIndexes: [
        {
            IndexName: 'fr_indx',
            KeySchema: [
                { AttributeName: "fr", KeyType: "HASH"},  //Partition key
                { AttributeName: "tm", KeyType: "RANGE" }  //Sort key
            ],
            AttributeDefinitions: [
                { AttributeName: "fr", AttributeType: "N" },
                { AttributeName: "tm", AttributeType: "N" }
            ],
            Projection: {
                ProjectionType: 'KEYS_ONLY'
            },
            ProvisionedThroughput: {
                ReadCapacityUnits: 10,
                WriteCapacityUnits: 10
            }
        }
    ]
};

dynamodb.createTable(messagesTableParams, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

But I keep getting the following error and the table is not created:

Unable to create table. Error JSON: { "message": "Unexpected key 'AttributeDefinitions' found in params.GlobalSecondaryIndexes[0]",
"code": "UnexpectedParameter", "time": "2016-01-07T18:51:11.659Z" }

Upvotes: 1

Views: 2992

Answers (2)

dtruong0
dtruong0

Reputation: 81

For extra clarity here is the fixed code example

var messagesTableParams = {
  TableName: "Messages",
  KeySchema: [
    { AttributeName: "to", KeyType: "HASH" }, //Partition key
    { AttributeName: "tm", KeyType: "RANGE" }, //Sort key
  ],
  AttributeDefinitions: [
    { AttributeName: "to", AttributeType: "N" },
    { AttributeName: "tm", AttributeType: "N" },
    { AttributeName: "fr", AttributeType: "N" },
    { AttributeName: "tm", AttributeType: "N" },
  ],
  ProvisionedThroughput: {
    ReadCapacityUnits: 10,
    WriteCapacityUnits: 10,
  },
  GlobalSecondaryIndexes: [
    {
      IndexName: "fr_indx",
      KeySchema: [
        { AttributeName: "fr", KeyType: "HASH" }, //Partition key
        { AttributeName: "tm", KeyType: "RANGE" }, //Sort key
      ],
      Projection: {
        ProjectionType: "KEYS_ONLY",
      },
      ProvisionedThroughput: {
        ReadCapacityUnits: 10,
        WriteCapacityUnits: 10,
      },
    },
  ],
};

Upvotes: 0

user2924127
user2924127

Reputation: 6242

I have finally figured it out! I need to remove the AttributeDefinitions from the global index and then add a new entry for the 'fr' attribute into the table's AttributeDefinition!

I would like to thank all the people who have supported me through this tough time and tolerated my inexcusable language towards my computer!

Upvotes: 6

Related Questions