I'm trying to return a a row with id (partition key)
var db = new AWS.DynamoDB.DocumentClient();
const id = "123";
var queryParams = {
TableName: "students",
IndexName: "id",
KeyConditionExpression: "id = :id",
ExpressionAttributeValues: {
":id": id,
},
};
const query = db.query(queryParams).promise();
but is throwing this error:
ValidationException: 1 validation error detected: Value 'id' at 'indexName' failed to satisfy constraint: Member must have length greater than or equal to 3
isn't partition key enough to get a record? I'm confused partition key is not the same as index name?
I also tried with
var db = new AWS.DynamoDB.DocumentClient();
var queryParams = {
TableName: "students",
Key: {
id: "123",
},
};
const query = db.get(queryParams).promise();
and it doesn't return anything
CodePudding user response:
You don't actually have an index named id
. You don't need to provide an index when querying on the partition key. Simply remove this line:
IndexName: "id",
Also, note that both table and index names must be 3-255 characters in length (inclusive), which explains the error message that you see.
The following code (both query and get) works fine for me:
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-1' });
const db = new AWS.DynamoDB.DocumentClient();
const ID = "123";
const TABLE_NAME = "students";
const getParams = {
TableName: TABLE_NAME,
Key: {
id: ID,
},
};
const queryParams = {
TableName: TABLE_NAME,
KeyConditionExpression: "id = :id",
ExpressionAttributeValues: {
":id": ID,
},
};
(async () => {
const rc1 = await db.get(getParams).promise();
console.log('get rc:', rc1);
const rc2 = await db.query(queryParams).promise();
console.log('query rc:', rc2);
})();