Home > Software engineering >  python boto3 DynamoDB validation error on getItem yet schema looks correct
python boto3 DynamoDB validation error on getItem yet schema looks correct

Time:05-21

Here is the scheme of my dynamoDB table

enter image description here

Here is the way I am requesting my get_item

if event['hcpcs_codes'] != None:
        # codes must be in numerical, alphabetical order
        # multi codes should be seperated by a comma with no spaces
        client = boto3.client('dynamodb')
        response = None
        try:
            response = client.get_item(TableName=os.environ['TABLE'],
                                   Key={'antecedents':{'S': str(event['hcpcs_codes'])}})
        except ValidationError as e:
            print(e)
        print('here comes the tacos brah')
        print(response)

but I get the error:

[ERROR] ClientError: An error occurred (ValidationException) when calling the GetItem operation: The provided key element does not match the schema
Traceback (most recent call last):
  File "/var/task/lambdafile.py", line 19, in lambda_handler
    Key={'antecedents':{'S': str(event['hcpcs_codes'])}})
  File "/var/task/botocore/client.py", line 415, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/task/botocore/client.py", line 745, in _make_api_call
    raise error_class(parsed_response, operation_name)

and according to the docs. It looks like Im doing this right

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Client.get_item

response = client.get_item(
    TableName='string',
    Key={
        'string': {
            'S': 'string',
            'N': 'string',
            'B': b'bytes',
            'SS': [
                'string',
            ],
            'NS': [
                'string',
            ],
            'BS': [
                b'bytes',
            ],
            'M': {
                'string': {'... recursive ...'}
            },
            'L': [
                {'... recursive ...'},
            ],
            'NULL': True|False,
            'BOOL': True|False
        }
    },
    AttributesToGet=[
        'string',
    ],
    ConsistentRead=True|False,
    ReturnConsumedCapacity='INDEXES'|'TOTAL'|'NONE',
    ProjectionExpression='string',
    ExpressionAttributeNames={
        'string': 'string'
    }
)

CodePudding user response:

You must supply both the partition key and the sort key when using GetItem if the table has a sort key set.

  • Related