PynamoDB
PynamoDB copied to clipboard
ValueError: No attribute <ATTRIBUTE_NAME> in ['hash_key', 'range_key']
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?
Any update on this?
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}})
I will try this - thank you.