Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB - Why can't I use an "_" as a prefix in my key condition expression?

I am using the AWS DynamoDB DocumentClient to query my DynamoDB tables. In my tables I have a property called "_id" that holds a unique ID for each entry. When I try to query for a record based on this ID, I end up receiving an error that says: "Invalid KeyConditionExpression: Syntax error; token: \"_\", near: \"_id\"". The code that makes this query is below.

    function findById(id) {

     //Build query
    var params = {};
    params.TableName = "recordDev";
    params.IndexName = "_id";
    params.KeyConditionExpression = '_id = :id';
    params.ExpressionAttributeValues = {
      ':id': id
    };

    return DynamoDB
        .query(params).promise()
        .then(function(records) {
          return records.Items[0];
        })
        .catch(function(error) {
          return Promise.reject(error);
        });
   };

Is there something that I am missing with regards to using an "_" when building the query params for DynamoDB? I have tried looking around for similar errors, but have been unable to find any that are like my scenario.

like image 686
tbergen1 Avatar asked Feb 13 '17 06:02

tbergen1


People also ask

What data types are valid for primary key of a DynamoDB table?

Each primary key attribute must be a scalar (meaning that it can hold only a single value). The only data types allowed for primary key attributes are string, number, or binary. There are no such restrictions for other, non-key attributes.

Does Amazon DynamoDB support conditional operations?

Yes, like all the other database management systems, DynamoDB also supports all the conditional operators, User can specify a condition that is satisfied for a put, update, or delete operation to work on an item.

Are DynamoDB keys case sensitive?

The following are the naming rules for DynamoDB: All names must be encoded using UTF-8, and are case-sensitive. Table names and index names must be between 3 and 255 characters long, and can contain only the following characters: a-z.

What is DynamoDB hash key?

DynamoDB supports two types of primary keys, a Hash Key and a Hash and Range Key. A Hash Key consists of a single attribute that uniquely identifies an item. A Hash and Range Key consists of two attributes that together, uniquely identify an item.


1 Answers

Can you try using the ExpressionAttributeNames param?

var params = {};
params.TableName = "recordDev";
params.IndexName = "_id";
params.KeyConditionExpression = '#id = :id';
params.ExpressionAttributeValues = {
  ':id': id
};
params.ExpressionAttributeNames: {
        "#id":"_id"
};
like image 169
gkatzioura Avatar answered Oct 26 '22 08:10

gkatzioura