[Python] SQLAlchemy: Abstract index

Discussão em 'Python' iniciado por Stack, Junho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I have this 2 abstract models:

    class BaseModel(DeclarativeBase):
    __abstract__ = True

    id: Mapped[int] = mapped_column(primary_key=True)
    created_at: Mapped[datetime.datetime] = mapped_column(
    TIMESTAMP(timezone=True), server_default=func.now()
    )
    updated_at: Mapped[datetime.datetime] = mapped_column(
    TIMESTAMP(timezone=True),
    server_default=func.now(),
    server_onupdate=func.now(),
    )


    class BaseLTreeModel(BaseModel):
    __abstract__ = True

    path: Mapped[LtreeType] = mapped_column(LtreeType, nullable=False)

    def __init_subclass__(cls, **kwargs):
    super().__init_subclass__(**kwargs)
    cls.__table_args__ = (
    Index(f"idx_{cls.__tablename__}_path", postgresql_using="gist"),
    )


    As you can see, I want have GIST index on all models using LTree. I tried this through __init_subclass__, so how can I do it?

    Continue reading...

Compartilhe esta Página