This repo demonstrates a custom SQLAlchemy column type that allows you to save and load Pydantic models directly in your SQLAlchemy database. It simplifies the process of storing and retrieving complex pydantic data models in your database without manual conversion to JSON or dictionaries.
In many projects, you might have complex data structures represented using Pydantic models. This custom SQLAlchemy column type, PydanticModelType
, enables you to store Pydantic model instances in your SQLAlchemy database. When you save data, it automatically serializes the Pydantic model to JSON, and when you retrieve data, it deserializes the JSON back to the Pydantic model.
To use this custom column type in your project, you'll need to:
- Include the
PydanticColumn
class while creating sqlalchemy ORMs model, as defined in custom_pydantic_column.py. - Define your Pydantic models, such as the
pydantic_model.py
example provided in the code.
- Import the
PydanticColumn
class from thecustom_pydantic_column.py
file. - Define your SQLAlchemy model and use the
PydanticColumn
column type for any Pydantic models you want to store in the database. - Insert Pydantic instances directly into the database, and they will be automatically serialized to JSON.
- When retrieving data from the database, the JSON will be deserialized back to Pydantic instances.
Here's an example of how to use this custom SQLAlchemy column type:
# Define a Pydantic model
# kindly check `src/pydantic_model.py`
class PydanticModel(BaseModel):
attr1: str
attr2: int
attr3: List[Another_Pydantic_Model]
# Create an SQLAlchemy model that uses PydanticModelType
# Kindly check `src/orms.py`
class ExampleModel(Base):
# ...
fiel21: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
filed2: Mapped[Python_Enums] = mapped_column(Enum(Python_Enums), nullable=False)
filed3: Mapped[Optional[PydanticModel]] = mapped_column(
PydanticColumn(PydanticModel), nullable=True
)
kindly check `dbio.py` for insert and select script