I have seen a structure as below
- models
- __init__.py
- user.py
models/__init__.py
from sqlalchemy.orm import declarative_base
Base = declarative_base()
from .user import User
and
models/user.py
from sqlalchemy.orm import Mapped, mapped_column
from . import Base
class User(Base):
__tablename__ = "user"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True, index=True)
username: Mapped[str] = mapped_column(index=True, unique=True)
slug: Mapped[str] = mapped_column(index=True, unique=True)
email: Mapped[str] = mapped_column(index=True, unique=True)
first_name: Mapped[str]
last_name: Mapped[str]
hashed_password: Mapped[str]
is_superuser: Mapped[bool] = mapped_column(default=False)
I can see Base
is imported in user.py
and again in __init__.py
user
is import
How will this be handled in SqlAlchemy
the above code i saw in: https://github.com/ThomasAitken/demo-fastapi-async-sqlalchemy/tree/main/backend/app/models