DynamoDB is a schemaless data store, so in general it is not necessary to declare attributes ahead of time.
The exception is any attribute that acts as a key either for the table itself (primary key, sort key) or for a secondary index. These must be defined as part of the definition of the table because DynamoDB needs to know how to structure the necessary indices to efficiently query by these attributes.
This is mentioned in more detail in the documentation:
Only define attributes on the table object that are going to be used
as:
- Table hash key or range key
- LSI or GSI hash key or range key
The three attributes UserId
, GameTitle
, and TopScore
from the example are used across the hash_key
, range_key
, and the GameTitleIndex
index respectively, and so they should be accepted.
To set an attribute that isn't a key, you just need to include it when writing the item to the table. DynamoDB will store that result and return it when the item is retrieved, but you will not be able to use it as a filter or sort key when querying the table.