laravel-dynamodb icon indicating copy to clipboard operation
laravel-dynamodb copied to clipboard

Invalid Query or Scan syntax with "single-table design" indexes

Open mnapoli opened this issue 11 months ago • 1 comments

Describe the bug

When querying a table designed with "single-table design" practices, the package fails to escape index names.

Schema

The standard names for secondary indexes would be:

                'GSI-1-PK': '...',
                'GSI-1-SK': '...',
                etc.

Here's an example of a query I do in JavaScript on that table:

        TableName: ...,
        IndexName: 'GSI-1',
        KeyConditionExpression: '#indexName = :PK',
        ExpressionAttributeValues: {
            ':PK': `...`,
        },
        ExpressionAttributeNames: {
            // The `-` character is a reserved character in DynamoDB, so we need to escape it
            '#indexName': 'GSI-1-PK',
        },

That's what ExpressionAttributeNames is here for (to alias attribute names).

I think this package needs to do the same thing and escape attribute names. Can you confirm that this isn't the case currently?

I get the following error:

Error executing "Query" on "https://dynamodb.us-east-1.amazonaws.com"; AWS HTTP error: Client error: POST https://dynamodb.us-east-1.amazonaws.com resulted in a 400 Bad Request response: {"__type":"com.amazon.coral.validate#ValidationException","message":"ExpressionAttributeNames contains invalid key: Synt (truncated...) ValidationException (client): ExpressionAttributeNames contains invalid key: Syntax error; key: "#GSI-1-PK" - {"__type":"com.amazon.coral.validate#ValidationException","message":"ExpressionAttributeNames contains invalid key: Syntax error; key: "#GSI-1-PK""}

Most important part:

Syntax error; key: "#GSI-1-PK"

DynamoDB doesn't like the - in the names.

Debug info

Show the query that you're having trouble with by copy-pasting the result of:

Route::get('/', function () {
    return view('home', [
        'measures' => Measure::query()
            ->where('GSI-1-PK', '...')
            ->where('GSI-1-SK', '*')
            ->get(),
    ]);
});

Version info

  • Laravel: latest
  • laravel-dynamodb: latest

mnapoli avatar Aug 10 '23 15:08 mnapoli

Hi. Try to enable debug in config and then run the query only.

There will be some debug logs of your query in your .log file under storage/logs/xxx.log (you can use daily type and debug as min level in the laravel logs config).

I'll look into it this weekend. 🧐

nelson6e65 avatar Aug 11 '23 04:08 nelson6e65