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 Table Resource is_in ConditionExpression overwrites data if more than 1 item is in the value list #4032

Open
KidAmnesiac1 opened this issue Feb 26, 2024 · 3 comments
Assignees
Labels
bug This issue is a confirmed bug. closing-soon This issue will automatically close in 4 days unless further comments are made. p2 This is a standard priority issue resources

Comments

@KidAmnesiac1
Copy link

Describe the bug

When using a ConditionalExpression is_in filter with more than one item in the filter value, the values used to filter overwrite data in the DynamoDB item

Expected Behavior

ConditionalExpressions should not change DynamoDB item

Current Behavior

Values in ConditionalExpression appear in DynamoDB item after update

Reproduction Steps

  • Create table via console called test_table with hash key test_key
  • Run the code below
import boto3
from boto3.dynamodb.conditions import Attr

ddb = boto3.resource("dynamodb").Table("test_table")

ddb.put_item(
    Item={
        "test_key": "foo-key",
        "filter_val": "foo",
        "val1": 123,
    },
)


resp = ddb.update_item(
    Key={"test_key": "foo-key"},
    UpdateExpression="SET #f1=:v1",
    ExpressionAttributeNames={"#f1": "val1"},
    ExpressionAttributeValues={":v1": 321},
    ConditionExpression=Attr("filter_val").is_in(["foo", "bar"]),
    ReturnValues="ALL_NEW",
)


print(resp["Attributes"])

# {'filter_val': 'foo', 'val1': 'bar', 'test_key': 'foo-key'}

see above the the value for val1 was originally set to 123, and was meant to be update to 321 but instead was updated to bar.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.34.49

Environment details (OS name and version, etc.)

Amazon Linux 2

@KidAmnesiac1 KidAmnesiac1 added bug This issue is a confirmed bug. needs-triage This issue or PR still needs to be triaged. labels Feb 26, 2024
@tim-finnigan tim-finnigan self-assigned this May 20, 2024
@tim-finnigan
Copy link
Contributor

Thanks for reaching out and your patience here. This update to your function appears to work for me. Can you confirm that this accomplishes your use case?

resp = ddb.update_item(
    Key={"test_key": "foo-key"},
    UpdateExpression="SET #f1=:v1",
    ExpressionAttributeNames={"#f1": "val1"},
    ExpressionAttributeValues={":v1": 321, ":valFoo": "foo", ":valBar": "bar"},
    ConditionExpression="filter_val IN (:valFoo, :valBar)",
    ReturnValues="ALL_NEW",
)

Linking is_in documentation for reference: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/dynamodb.html#boto3.dynamodb.conditions.Attr.is_in

@tim-finnigan tim-finnigan added response-requested Waiting on additional information or feedback. investigating This issue is being investigated and/or work is in progress to resolve the issue. resources p2 This is a standard priority issue 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 20, 2024
@KidAmnesiac1
Copy link
Author

@tim-finnigan Thanks for getting back to me.

Yes, this modification works.

Am I misinterpreting the docs is seems using boto3.dynamodb.conditions.Attr is preferred to strings.

It would be good if is_in behaved like your example as it would allow for cleaner/easier composability of filters vs. string manipulation.

@github-actions github-actions bot removed the response-requested Waiting on additional information or feedback. label May 30, 2024
@tim-finnigan
Copy link
Contributor

Thanks for following up, it looks like this updating adding filter_val to ExpressionAttributeNames does what you're looking for:

resp = ddb.update_item(
    Key={"id": "foo-key"},
    UpdateExpression="SET #f1=:v1",
    ExpressionAttributeNames={"#f1": "filter_val"},
    ExpressionAttributeValues={":v1": 321},
    ConditionExpression=Attr("filter_val").is_in(["foo", "bar"]),
    ReturnValues="ALL_NEW",
)

@tim-finnigan tim-finnigan added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jun 5, 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. closing-soon This issue will automatically close in 4 days unless further comments are made. p2 This is a standard priority issue resources
Projects
None yet
Development

No branches or pull requests

2 participants