Moto: GetItem throws ValidationException

Created on 28 Aug 2019  路  7Comments  路  Source: spulec/moto

I have a table with the schema:

table = dynamodb.create_table( TableName=table_name, KeySchema=[ { 'AttributeName': 'version', 'KeyType': 'HASH' # Partition key }, { 'AttributeName': 'tid', 'KeyType': 'RANGE' # Sort key } ], AttributeDefinitions=[ { 'AttributeName': 'version', 'AttributeType': 'S' }, { 'AttributeName': 'tid', 'AttributeType': 'N' }, ], BillingMode='PAY_PER_REQUEST' )
When one tries to get an item from the table, one ends up with the following error:
botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the GetItem operation: Validation Exception
The code:
response = table.get_item(Key = { "tid":tid, "version":version })

debugging

Most helpful comment

is there any fix for this? I'm getting the same error

All 7 comments

Hi,

could give some details on your problem, which moto version are using and the full example code? It works me fine for me, so what exactly are the variables tid and version? Is an item already inside the table?

Hi,
Sorry for the late response. tid and version are both strings, and the table is initally empty.

Ok, your main mistake is, that you try to call get_item() with tid as a string, but you defined it to be N a Number. Either you change your AttributeDefinitions to

table = dynamodb.create_table(
    ...
    AttributeDefinitions = [{
        'AttributeName': 'version',
        'AttributeType': 'S'
    }, {
        'AttributeName': 'tid',
        'AttributeType': 'S'
    }]
)

or change tid to any numeric value.
Anyway the behavior of moto is also wrong here, it doesn't validate the AttributeDefinitions against any follow up request.

is there any fix for this? I'm getting the same error

I'm going to close this item, as the original issue was solved (by using the correct AttributeType.

@alok2k5ingh Feel free to open a new issue if you feel anything else is still bugged, ideally with a minimal reproducable test :)

@scottplawrence Definitely open a new issue for this - I can't reproduce your example, so that might need some more debugging.

@bblommers I did some more debugging and the error I was receiving was a query on another table where the table name had a typo - sorry for the confusion!

I have deleted my original comment to avoid further searches.

Just a note for those who still may encounter this error and stumble upon this post: pay attention to the AWS region when setting up the mock tables in your test file.

I received the ValidationException when I created my mock table in the eu-west-2 region, but my default region (from .aws/config on my Mac) was set to the ap-southeast-2 region. When the code is tested, it cannot find the mock table in the default region, so throws the exception.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eliasdorneles picture eliasdorneles  路  5Comments

muhammad-ammar picture muhammad-ammar  路  3Comments

bjmc picture bjmc  路  3Comments

mdavis-xyz picture mdavis-xyz  路  4Comments

domingosnovo picture domingosnovo  路  5Comments