PynamoDB icon indicating copy to clipboard operation
PynamoDB copied to clipboard

ValueError: No attribute <ATTRIBUTE_NAME> in ['hash_key', 'range_key']

Open jasonstrimpel opened this issue 2 years ago • 2 comments

Greetings,

I have existing DynamoDB tables created through an infrastructure-as-code implementation.

I used TableConnection to connect to the existing table and run the lower level command put_item.

My table is defined with a hash key (userId) and sort key (transactionId).

I want to put an item with additional attributes (timestamp, symbol, quantity).

I have not defined these additional attributes.

When I run the following command:

table = TableConnection('ExistingTable')
table.put_item(user_id, transaction_id, attributes={...})

I receive the following error:

ValueError: No attribute timestamp in ['userId', 'transactionId']

How do I put an item without defining the attributes previously?

jasonstrimpel avatar May 25 '22 01:05 jasonstrimpel

Any update on this?

OGoodness avatar Jul 08 '22 00:07 OGoodness

You wrote:

table.put_item(user_id, transaction_id, attributes={...})

which appears to omit the actual value passed as the attributes parameter (I assume it's not a set containing a single ellipsis item...).

If you did something like:

table.put_item(user_id, transaction_id, attributes={"timestamp": 123})

then you're making an assumption that you cannot make with DynamoDB since it's schema-less: we can't assume the type of 123. PynamoDB tries to (helpfully?) find the type if it was defined in the table definition (i.e. if it's a key) but if it's not then it cannot make assumptions.

You can instead do:

table.put_item(user_id, transaction_id, attributes={"timestamp": {"N": 123}})

ikonst avatar Jul 08 '22 15:07 ikonst

I will try this - thank you.

jasonstrimpel avatar Aug 15 '22 01:08 jasonstrimpel