Skip to content

boto3: where is attribute_not_exists function documented?

0

I'd like my lambda to add a new item to a dynamo db table using 'put_item'. I'd like to check before adding the item that an item in the table with the same primary key as the identifier for the new item I've generated does not exist before adding the item. Here's what the boto3 docs say about how to do that:

Enter image description here

That's nice, but it doesn't give me a link to any documentation about "the attributes_not_exists function", whatever that is.

Where is that function documented?

asked 2 years ago2.4K views

3 Answers
2

Do a Ctrl-F and search for the term further down the same doc.

ConditionExpression (string) – A condition that must be satisfied in order for a conditional PutItem operation to succeed.
An expression can contain any of the following:
Functions: attribute_exists | attribute_not_exists | attribute_type | contains | begins_with | size These function names are case-sensitive.
Comparison operators: = | <> | < | > | <= | >= | BETWEEN | IN
Logical operators: AND | OR | NOT
For more information on condition expressions, see Condition Expressions in the Amazon DynamoDB Developer Guide.

To use it fit it in the overall call in the ConditionExpression

response = client.put_item(
    TableName='string',
    Item={
        'string': {
            'S': 'string',
            'N': 'string',
            'B': b'bytes',
            'SS': [
                'string',
            ],
...
    ConditionalOperator='AND'|'OR',
    ConditionExpression='string',
    ExpressionAttributeNames={
        'string': 'string'
    },
...
)
AWS
EXPERT

answered 2 years ago

AWS
EXPERT

reviewed 2 years ago

1

Hi,

This sample (adapted to your specific needs) should work for you:

try:
    table.put_item(
        Item={
            'foo':1,
            'bar':2,
        },
        ConditionExpression='attribute_not_exists(foo) AND attribute_not_exists(bar)'
    )
except botocore.exceptions.ClientError as e:
    # Ignore the ConditionalCheckFailedException, bubble up
    # other exceptions.
    if e.response['Error']['Code'] != 'ConditionalCheckFailedException':
        raise

Best,

Didier

EXPERT

answered 2 years ago

AWS
EXPERT

reviewed 2 years ago

-3
Accepted Answer
EXPERT

answered 2 years ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.