Bug: When the name of the hash key is incorrect the update method fails with non-descriptive error
The issue I had:
I created a model to reflect my db. The hash key was set on an attribute which was named incorrectly. Example below
# What it looked like
class Thread(Model):
class Meta:
table_name = "Thread"
forum_name = UnicodeAttribute(hash_key=True)
subject = UnicodeAttribute(range_key=True)
views = NumberAttribute(default=0)
# What it should have looked like
class Thread(Model):
class Meta:
table_name = "Thread"
actual_forum_name = UnicodeAttribute(hash_key=True)
subject = UnicodeAttribute(range_key=True)
views = NumberAttribute(default=0)
When performing a get() on this model it would return the correct data.
When an update() operation was to be performed on that returned record it would throw an error.
record = Thread.get("github")
# works fine ^
record.update(
actions=[
Thread.subject.set("issue")
]
)
# throws error ^
The error it would throw would be directly from aws.
Failed to update item: An error occurred (ValidationException) on request (xxx) on the table (xxx) when calling the UpdateItem operation: Supplied AttributeValue is empty, and must contain exactly one of the supported datatypes
What caused it:
When going into the source code and printing out the api call it is trying to make I found the following request.
UpdateItem {'TableName': 'xxx', 'Key': {'forum_name': {'S': None}}, 'ReturnValues': 'ALL_NEW', 'UpdateExpression': 'SET #0 = :0', 'ExpressionAttributeNames': {'#0': 'subject'}, 'ExpressionAttributeValues': {':0': {'S': 'issue'}}, 'ReturnConsumedCapacity': 'TOTAL'}
This is because it is trying to get the real hash key value "actual_forum_name" which is None.
Outcome
I would have submitted a PR to "fix" this issue. But I am unsure if the community here would classify this as an issue. It only happens when you create an incorrect model and therefore would never work.
But on the other hand, the package should throw a KeyError when trying to get the hash key instead of allowing it to be None and showing an unhelpful aws error message.
If people could let me know if people can replicate this error and how it (if) it should be handled.
System Information
pynamodb==5.2.1 python==3.8.9