I'm creating a user table in dynamo db and want to utilize pynamodb ORM, sample class below.
pynamodb class
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute
class UserModel(Model):
class Meta:
table_name = "user"
first_name = UnicodeAttribute(range_key=True)
last_name = UnicodeAttribute(hash_key=True)
email = UnicodeAttribute(null=True)
I also have a pydantic class for data validation , see below. which validates the data, when client posts data to my REST API. Now i need to update my dynamo db table , to add another attribute , say age. I understand dynamodb doesn't really have a schema, per say. so we can add/update any attributes in the table, but when we use pynamodb ORM, do i just update the UserModel class above? add another attribute age to my UserModel class and also to my pydantic class. is there anything i need to update?
from datetime import date
from uuid import UUID, uuid4
from enum import Enum
from pydantic import BaseModel, EmailStr
class User(BaseModel):
first_name : str
last_name : str
email: str
I want to update
Update your UserModel class with a new attribute and then reflect the change to your pydantic class.
from pynamodb.models import Model
from pynamodb.attributes import UnicodeAttribute, NumberAttribute
class UserModel(Model):
class Meta:
table_name = "user"
first_name = UnicodeAttribute(range_key=True)
last_name = UnicodeAttribute(hash_key=True)
email = UnicodeAttribute(null=True)
age = NumberAttribute(null=True) # New attribute
from pydantic import BaseModel, EmailStr
from typing import Optional
class User(BaseModel):
first_name: str
last_name: str
email: Optional[EmailStr] = None
age: Optional[int] = None # New attribute
Consider backfilling your existing data