boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
8.99k stars 1.86k forks source link

DynamoDB: No way to combine multiple ConditionExpression #4112

Closed SamStephens closed 4 months ago

SamStephens commented 4 months ago

Describe the feature

According to https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html it's possible to combine multiple conditions using AND and OR. However, from my reading of https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/table/update_item.html and https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/dynamodb.html#boto3.dynamodb.conditions.Attr, it is only possible to specify a single condition using the update_item operation.

Use Case

I have an attribute I want to update, but not if the update will decrease the attributes value. The attribute is not necessarily present. So I need my conditional expression to be attribute not exists OR attribute value < my value.

It appears the API supports this conditional expression, but not this SDK.

Proposed Solution

Under this scheme, my ConditionExpression would be

ConditionExpression = Condition().Or(Attr(attribute_name).not_exists(), Attr(attribute_name).lt(updated_value))

Other Information

The only way I can see to workaround this is two conditional updates - firstly with not_exists, and then if the check fails because the attribute exists, try the update again with lt.

Acknowledgements

SDK version used

1.34.76

Environment details (OS name and version, etc.)

Ubuntu (Windows Subsystem for Linux)

crh23 commented 4 months ago

Do the classes defined as follows fit your use-case? https://github.com/boto/boto3/blob/096e45841545f24bbd572a62504cc0dbb62e6b07/boto3/dynamodb/conditions.py#L158-L238 You can write expressions like ConditionExpression = Or(Attr(attribute_name).not_exists(), Attr(attribute_name).lt(updated_value))

I'm not sure if they are documented anywhere

SamStephens commented 4 months ago

@crh23 thanks, this is exactly what I needed.

I've closing this issue as support is present, and replacing it with an issue for the missing documentation.

github-actions[bot] commented 4 months ago

This issue is now closed. Comments on closed issues are hard for our team to see. If you need more assistance, please open a new issue that references this one.