Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

class Log(Base): tablename = "log" id: so.Mapped[int] = mappedcolumn(primarykey=True)…

  1. 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)?
    
  2. Assistant

    No, you do not need to include a primary key column when using the mapped_column function. The id column is already defined as the primary key using the primary_key=True argument in its declaration.

  3. 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()?

  4. Assistant

    No, you do not need to define Base = declarative_base() in both tables.py and config.py. It is better to define it only in one place and import it where necessary. You can define Base in tables.py and then import it into config.py with from src.models.tables import Base.

  5. User

    Why I need it in config.py? It is not used

  6. Assistant

    You may not need to define Base in config.py if it is not used there. It is only necessary to define Base in config.py if you are using it or inheriting from it in that file. If Base is only used in tables.py, you can import it from there into config.py without defining it again.

  7. User

    Why I need import if it is not using in cionfig.py

  8. Assistant

    You do not need to import Base from tables.py into config.py if it is not used in config.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 using Base in config.py, you can remove the import statement.