65

Is there a way to get an item depending on a field that is not the hashkey?

Example

My Table Users: id (HashKey), name, email

And I want to retrieve the user having email as 'test@mail.com'

How this can be done?

I try this with boto:

user = users.get_item(email='john.doe@gmail.com')

I get the following error:

'The provided key element does not match the schema'
tripleee
  • 175,061
  • 34
  • 275
  • 318

10 Answers10

57

The following applies to the Node.js AWS SDK in the AWS Lambda environment:

This was a rough one for me. I ran into this problem when trying to use the getItem method. No matter what I tried I would continue to receive this error. I finally found a solution on the AWS forum: https://forums.aws.amazon.com/thread.jspa?threadID=208820

Inexplicably, the apparent solution conflicts with all AWS documentation that I can find.

Here is the code which worked for me:

var doc = require('dynamodb-doc');
var dynamo = new doc.DynamoDB();

var params = { }
params.TableName = "ExampleTable";
var key = { "ExampleHashKey": "1" };
params.Key = key;

dynamo.getItem(params, function(err, data) {
    if (err)
        console.log(err);
    else
        console.log(data)
});
Sean
  • 571
  • 1
  • 4
  • 2
  • 25
    Thank you! The problem is indeed that the AWS documentation asks for a marshalled object (e.g. `var key = { "ExampleHashKey": { "S": "1" } }`), while the actual SDK expects a normal javascript object (e.g. `var key = { "ExampleHashKey": "1" }`). This was also happening for the `update`/`updateItem` method – aviggiano Apr 19 '18 at 14:03
  • 16
    The documentation for DynamoDB has always been a challenge. I think the root cause of the problem here is the distinction between the base DynamoDB client and the higher-level Document Client. The base client uses AttributeValues e.g. var key = { S: { name: 'joe' } } while the Document Client uses native JavaScript objects e.g. var key = { name: 'joe' }. – jarmod Sep 03 '18 at 22:36
  • In the case of the Ruby docs https://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#get_item-instance_method seems to be confused because the example for the Aws::DynamoDB::Client class contains the same issue of describing a marshaled value but actual expects to handle a typical hash – Mark Fox Oct 26 '18 at 06:14
  • ExampleHashKey is the id of the data entry. What does the 1 next to it stand for – gorilla bull Mar 05 '19 at 21:18
  • 2
    Note: apparently this solution no longer works, and now the AWS SDK behaves in accordance with the documentation, so your example would be correct in this form now: `var key = { "ExampleHashKey": { S: "1"} };` – gregn3 May 17 '20 at 21:10
25

To query on fields which are not the hash key you need to use a Global Secondary Index (GSI). Take a look at this AWS Post for more details on GSI's.

UPDATE Feb 2015: It is now possible to add a GSI to an existing table. See the Amazon Docs for more details.

Sadly you cannot add a GSI to an existing DynamoDB table so you'll need to create a new table and port your data if this is something you really need to query on.

From the DynamoDB FAQ:

Q: How do I create a global secondary index for a DynamoDB table?

All GSIs associated with a table must be specified at table creation time. At this time, it is not possible to add a GSI after the table has been created. For detailed steps on creating a Table and its indexes, see here. You can create a maximum of 5 global secondary indexes per table.

If you do not want to port your data you could consider creating a second DynamoDB table with the email as a hash key and the hash of the parent record to use as a lookup into your main data table but as you can imagine this isn't exactly an optimal solution and it comes with its own headaches of keeping it in synch with your master table.

Community
  • 1
  • 1
Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
  • Thanks Wolfwyrd.. a lot of things are clear for me now. –  Sep 17 '14 at 09:21
  • 3
    This is an older response, but just in case someone finds this answer, it is now possible to create Global indexes on an existing table. Local indexes must still be set up at creation time. – Kris White Feb 08 '15 at 17:05
15

I also got this error when I was sending a string instead of an integer.

Of course, this was when I was writing to the database, rather than reading from.

NotoriousWebmaster
  • 3,298
  • 7
  • 37
  • 49
  • in my case using baopham/laravel-dynamodb I have to make an explicit type using (string) and (integer): `$model = ModelData::where('PartitionKey', (string)$SearchPartitionKey)->where('SortKey',(integer)$SearchSortKey)->get();` – Cristian Sepulveda Jun 08 '17 at 22:16
12

I have a partition key and a sort key on my table.

I was querying it with only the partition key, and I got this error.

Obviously, querying the composite key fixed it.

Ben
  • 54,723
  • 49
  • 178
  • 224
3

When you create a table, There is a field for 'partition key' and a field for 'sort key (optional)'. If you enter something in the 'sort key' field, you need to use it in your calls by primary key such as get_item.

For example if my partition key is 'name' and my sort key is 'surname', I need to call it like this:

var params = {
  TableName: 'mytable',
  Key: {
    'name': {S: 'John'},
    'surname': {S: 'Sena'}
  }
};

If you make a get_item call to this table only with the first part of the primary key (name), you get the 'The provided key element does not match the schema.' error because its missing the surname.

To fix it, add the sort key in your call (or recreate the table without a sort key).

L777
  • 7,719
  • 3
  • 38
  • 63
2

I got this error in Java because I had used the @DynamoDBHashKey annotation for a RANGE key. I had to use the @DynamoDBRangeKey annotation for my object's id instead.

Ryan Shillington
  • 23,006
  • 14
  • 93
  • 108
1

I got this error in my Java application, because I accidentally had two Range keys annotated (@DynamoDBRangeKey) in my DAO class, when there should only be one. I changed the new addition's annotation to @DynamoDBAttribute, and that solved it.

Josh
  • 71
  • 8
1

I got this error in my AWS Lambda function, because my parameters was BigInt and needed to be converted to Number. I changed this:

let data = await docClient.get({ TableName: "items", Key: { "accountid": accountId, "itemid": itemId } }).promise();

... to this:

let data = await docClient.get({ TableName: "items", Key: { "accountid": Number(accountId), "itemid": Number(itemId) } }).promise();
Fredrik Johansson
  • 3,477
  • 23
  • 37
1

In case it helps anyone else just starting out with DynamoDB, I got this error when trying to use GetItemCommand and only providing a Partition key on a table with a composite Primary key.

Either additionally specify a value for the Sort key to retrieve the single record that matches that combination, or switch to QueryCommand to return anything matching just the partition key regardless of the Sort key.

Mike D Sutton
  • 716
  • 3
  • 9
0

I received this error when using a field from the result of a DynamoDB get call directly as the key to another get call. The issue was that I didn't use .Item so when the code dynamoResult.fieldName was used as the key it would evaluate to undefined, not the value I needed. This explains the error as undefined is not a string (which is the type the HASH key was set to).

To resolve this I changed dynamoResult.fieldName to dynamoResult.Item.fieldName

Benjamin Sommer
  • 1,058
  • 3
  • 15
  • 35