class Log(Base): tablename = "log" id: so.Mapped[int] = mappedcolumn(primarykey=True)…
- User
class Log(Base): tablename = "log"
id: so.Mapped[int] = mapped_column(primary_key=True) request_datetime: so.Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False) request_url: so.Mapped[str] = mapped_column(Text, nullable=False) request_method: so.Mapped[str] = mapped_column(String(8), nullable=False) response_datetime: so.Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False) response_status_code: so.Mapped[int] = mapped_column(SmallInteger, nullable=False) response_error_type: so.Mapped[str] = mapped_column(String(2), nullable=False) response_error_number: so.Mapped[str] = mapped_column(String(2), nullable=False) response_error_code: so.Mapped[str] = mapped_column(String(5), nullable=True) phone_number: so.Mapped[str] = mapped_column(String(10), nullable=True) duration: so.Mapped[timedelta] = mapped_column(Interval, nullable=False) do I need write mapped_column(Integer, primary_key=True)? - Assistant
No, you do not need to include a primary key column when using the
mapped_columnfunction. Theidcolumn is already defined as the primary key using theprimary_key=Trueargument in its declaration. - User
I have tables.py with from datetime import timedelta, datetime
from sqlalchemy import SmallInteger, String, TIMESTAMP, Text, Interval from sqlalchemy.orm import DeclarativeBase, mapped_column from sqlalchemy.orm.base import Mapped
class Base(DeclarativeBase): pass
class Log(Base): tablename = "log"
id: Mapped[int] = mapped_column(primary_key=True) request_datetime: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False) request_url: Mapped[str] = mapped_column(Text, nullable=False) request_method: Mapped[str] = mapped_column(String(8), nullable=False) response_datetime: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), nullable=False) response_status_code: Mapped[int] = mapped_column(SmallInteger, nullable=False) response_error_type: Mapped[str] = mapped_column(String(2), nullable=False) response_error_number: Mapped[str] = mapped_column(String(2), nullable=False) response_error_code: Mapped[str] = mapped_column(String(5), nullable=True) phone_number: Mapped[str] = mapped_column(String(10), nullable=True) duration: Mapped[timedelta] = mapped_column(Interval, nullable=False)log = Log.table ; and config.py from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker from sqlalchemy.ext.declarative import declarative_base
from src.settings.app_settings import settings
Base = declarative_base()
engine = create_async_engine(settings.db_url, echo=True) async_session_maker = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async def get_async_session() -> AsyncGenerator[AsyncSession, None]: async with async_session_maker() as session: yield session; do I need this one in both Base = declarative_base()?
- Assistant
No, you do not need to define
Base = declarative_base()in bothtables.pyandconfig.py. It is better to define it only in one place and import it where necessary. You can defineBaseintables.pyand then import it intoconfig.pywithfrom src.models.tables import Base. - User
Why I need it in config.py? It is not used
- Assistant
You may not need to define
Baseinconfig.pyif it is not used there. It is only necessary to defineBaseinconfig.pyif you are using it or inheriting from it in that file. IfBaseis only used intables.py, you can import it from there intoconfig.pywithout defining it again. - User
Why I need import if it is not using in cionfig.py
- Assistant
You do not need to import
Basefromtables.pyintoconfig.pyif it is not used inconfig.py. When you are not using a module or any of its contents in a file, you do not need to import it. So, if you are not usingBaseinconfig.py, you can remove the import statement.