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

Dynamodb transact_write_items throws a exception validating ConditionExpression #4053

Open
LiamCato opened this issue Mar 19, 2024 · 4 comments
Assignees
Labels
bug This issue is a confirmed bug. dynamodb p2 This is a standard priority issue

Comments

@LiamCato
Copy link

LiamCato commented Mar 19, 2024

Describe the bug

When using an object supplied from from boto3.dynamodb.conditions import Attr rather than string expression transact_write_items raises an exception:

Unknown parameter in input: "ExpressionAttributeNames", must be one of: TransactItems, ReturnConsumedCapacity, ReturnItemCollectionMetrics, ClientRequestToken

When a ConditionExpression is supplied for a put.

Expected Behavior

I expect the transact_write_items call to function the same where the string representation of a ConditionExpression is supplied or the python object is supplied.

Current Behavior

The string expression works as expected, whereas when using the Attr object, the inject_condition_expressions function unpacks the api_params into:

{'TransactItems': [{'Put': {'TableName': 'my-table', 'Item': {'PK': {'S': 'Test'}, 'SK': {'S': 'Test'}, 'data': {'S': 'Test'}}, 'ConditionExpression': 'attribute_not_exists(#n0)'}}], 'ExpressionAttributeNames': {'#n0': 'SK'}, 'ClientRequestToken': 'c55280d1-da03-4d4f-9096-2cb6b9b2ff97'}

Note how the ExpressionAttributeNames is now outside the item and is validated as part of the top level members which causes the "Unknown parameter in input" exception.

Reproduction Steps

Example:

Works:

from boto3 import resource
dynamodb = resource("dynamodb")
table = dynamodb.Table("mytable")
body = [{'Put': {'TableName': 'my-table-name', 'Item': {'PK': 'Test', 'SK': 'Test', 'data': 'Test'}, 'ConditionExpression': 'attribute_not_exists(SK)' }}]
table.meta.client.transact_write_items(TransactItems=body)

Same action using the Attr object does not work:

from boto3 import resource
from boto3.dynamodb.conditions import Attr
dynamodb = resource("dynamodb")
table = dynamodb.Table("mytable")
body = [{'Put': {'TableName': 'my-table-name', 'Item': {'PK': 'Test', 'SK': 'Test', 'data': 'Test'}, 'ConditionExpression': Attr("SK").not_exists()}}]
table.meta.client.transact_write_items(TransactItems=body)

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.34.65

Environment details (OS name and version, etc.)

Ubuntu 22.04, python 3.11

@LiamCato LiamCato added bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels Mar 19, 2024
@LiamCato
Copy link
Author

LiamCato commented Mar 19, 2024

I've created an example pull request that would fix this. I am not attached to it, it's just a suggestion and I'm happy to throw it away it the maintainers don't like it.

I do wonder however why all the expressions are not unpacked during the recursion like in my change, rather than putting just one of the transforms attributes there and then storing placeholders for later?

@tim-finnigan tim-finnigan self-assigned this May 22, 2024
@tim-finnigan tim-finnigan added the investigating This issue is being investigated and/or work is in progress to resolve the issue. label May 22, 2024
@tim-finnigan
Copy link
Contributor

Thanks for reaching out and your patience here. I'm not fully clear on the issue here and think we need some more information.

Both code snippets you provided fail with the error: AttributeError: 'dynamodb.Table' object has no attribute 'transact_write_items'. Here is the documentation for that client method: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/client/transact_write_items.html

Can you provide new code snippets highlighting the issue and what you're trying to do? Adding debug logs using boto3.set_stream_logger('') (with sensitive info redacted) may also give more insight here.

@tim-finnigan tim-finnigan added response-requested Waiting on additional information or feedback. p2 This is a standard priority issue dynamodb and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. needs-triage This issue or PR still needs to be triaged. labels May 22, 2024
@LiamCato
Copy link
Author

@tim-finnigan

Ooops, sorry about that, I foolishly used a resource instead of a client in the example. I've updated the example to use the client and tested the snippet to check it gives the error I've reported.

@LiamCato
Copy link
Author

Hopefully my example change and test in the linked PR should give you insight into where in the code the issue is.

https://github.com/boto/boto3/pull/4054/files

@github-actions github-actions bot removed the response-requested Waiting on additional information or feedback. label May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a confirmed bug. dynamodb p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants