Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidationException not detected on DynamoDB query #8655

Open
merinhunter opened this issue Mar 6, 2025 · 0 comments
Open

ValidationException not detected on DynamoDB query #8655

merinhunter opened this issue Mar 6, 2025 · 0 comments

Comments

@merinhunter
Copy link

Bug description

Making a query to a DynamoDB Table Index with two different key values throws the following error:

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the Query operation: Query key condition not supported

However, if the same query is made against a mocked environment with moto this exception is never raised.

To test this behavior, I used the following script (comment the the mock_aws statement to execute against AWS):

import boto3
from moto import mock_aws

table_name = 'Threads'
forum_name = 'ForumName'
subject = 'Subject'
author = 'Author'
index_name = 'Author'
table_definition = {
    "AttributeDefinitions": [
        {
            'AttributeName': forum_name,
            'AttributeType': 'S'
        },
        {
            'AttributeName': subject,
            'AttributeType': 'S'
        },
        {
            'AttributeName': author,
            'AttributeType': 'S'
        }
    ],
    "TableName": table_name,
    "KeySchema": [
        {
            'AttributeName': forum_name,
            'KeyType': 'HASH'
        },
        {
            'AttributeName': subject,
            'KeyType': 'RANGE'
        }
    ],
    "GlobalSecondaryIndexes": [
        {
            'IndexName': index_name,
            'KeySchema': [
                {
                    'AttributeName': author,
                    'KeyType': 'HASH'
                }
            ],
            'Projection': {
                'ProjectionType': 'ALL'
            }
        }
    ],
    "BillingMode": 'PAY_PER_REQUEST',
}


@mock_aws
def create_load_query_delete(deleteTable=False):
    ddb_client = boto3.client("dynamodb")

    try:
        ddb_client.create_table(**table_definition)
        waiter = ddb_client.get_waiter('table_exists')
        waiter.wait(TableName=table_name)
    except ddb_client.exceptions.ResourceInUseException:
        print("Table exists not creating.")

    ddb_client.put_item(
        TableName=table_name,
        Item={
            forum_name: {'S': 'forum1'},
            subject: {'S': 'subject1'},
            author: {'S': 'author1'}
        }
    )

    ddb_client.put_item(
        TableName=table_name,
        Item={
            forum_name: {'S': 'forum1'},
            subject: {'S': 'subject2'},
            author: {'S': 'author2'}
        }
    )

    query_args = {
        'TableName': table_name,
        'IndexName': index_name,
        'KeyConditionExpression': f'{author} = :author AND {subject} = :subject',
        'ExpressionAttributeValues': {
            ':author': {
                'S': 'author1'
            },
            ':subject': {
                'S': 'subject1'
            }
        }
    }

    response = ddb_client.query(**query_args)
    items = response.get('Items', [])

    if deleteTable:
        ddb_client.delete_table(TableName=table_name)

    return items


def test_query():
    assert len(create_load_query_delete(deleteTable=True)) == 1

Environment details

  • moto: 5.0.28
  • boto3: 1.36.26
  • python: 3.12.9
  • OS: Ubuntu 22.04.5 LTS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant