I'm following this tutorial to adapt it to my needs, in this case, to perform a sql module where I need to record the data collected by a webhook from the gitlab issues.
For the database module I'm using SQLAlchemy library and PostgreSQL as database engine.
So, I would like to solve some doubts, I have regarding the use of the Pydantic library, in particular with this example
From what I've read, Pydantic is a library that is used for data validation using classes with attributes.
But I don't quite understand some things...is the integration of Pydantic strictly necessary? The purpose of using Pydantic I understand, but the integration of using Pydantic with SQLAlchemy models I don't understand.
In the tutorial, models.py
has the following content:
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from .database import Base
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True)
hashed_password = Column(String)
is_active = Column(Boolean, default=True)
items = relationship("Item", back_populates="owner")
class Item(Base):
__tablename__ = "items"
id = Column(Integer, primary_key=True, index=True)
title = Column(String, index=True)
description = Column(String, index=True)
owner_id = Column(Integer, ForeignKey("users.id"))
owner = relationship("User", back_populates="items")
And schemas.py
has the following content:
from typing import Optional
from pydantic import BaseModel
class ItemBase(BaseModel):
title: str
description: Optional[str] = None
class ItemCreate(ItemBase):
pass
class Item(ItemBase):
id: int
owner_id: int
class Config:
orm_mode = True
class UserBase(BaseModel):
email: str
class UserCreate(UserBase):
password: str
class User(UserBase):
id: int
is_active: bool
items: list[Item] = []
class Config:
orm_mode = True
I know that the primary means of defining objects in Pydantic is via models and also I know that models are simply classes which inherit from BaseModel.
Why does it create ItemBase, ItemCreate and Item that inherits from ItemBase?
In ItemBase it passes the fields that are strictly necessary in Item table? and defines its type?
The ItemCreate class I have seen that it is used latter in crud.py
to create a user, in my case I would have to do the same with the incidents? I mean, I would have to create a clase like this:
class IssueCreate(BaseModel):
pass
There are my examples trying to follow the same workflow:
models.py
import sqlalchemy
from sqlalchemy import Column, Table
from sqlalchemy import Integer, String, Datetime, TIMESTAMP
from .database import Base
class Issues(Base):
__tablename__ = 'issues'
id = Column(Integer, primary_key=True)
gl_assignee_id = Column(Integer, nullable=True)
gl_id_user = Column(Integer, nullable=False)
current_title = Column(String, nullable=False)
previous_title = Column(String, nullable=True)
created_at = Column(TIMESTAMP(timezone=False), nullable=False)
updated_at = Column(TIMESTAMP(timezone=False), nullable=True)
closed_at = Column(TIMESTAMP(timezone=False), nullable=True)
action = Column(String, nullable=False)
And schemas.py
from pydantic import BaseModel
class IssueBase(BaseModel):
updated_at: None
closed_at: None
previous_title: None
class Issue(IssueBase):
id: int
gl_task_id: int
gl_assignee_id: int
gl_id_user: int
current_title: str
action: str
class Config:
orm_mode = True
But I don't know if I'm right doing it in this way, any suggestions are welcome.