Type annotations for sqlalchemy model declaration
Asked Answered
H

1

5

I can't figure how i could type annotate my sqlalchemy models code, what kind of type should i use for my model fields.

class Email(Model):
    __tablename__ = 'emails'

    name: Column[str] = Column(String, nullable=False)
    sender: Column[str] = Column(
        String, default=default_sender, nullable=False
    )
    subject: Column[str] = Column(String, nullable=False)
    html: Column[str] = Column(String, nullable=False)
    template_id: Column[UUID] = Column(
        postgresql.UUID(as_uuid=True),
        ForeignKey('templates.id'),
        index=True,
    )

Column type as well as Mapped satisfies the type linter of my editor, but it doesn't look like the right one as opposed to simple types like str/int/uuid

Should i use Optional[Column[str]] or Column[Optional[str]]?

What to use for relationship?

class Foo(Model):
    ...
    bar: Bar = relationship(
        'Bar',
        order_by="desc(Bar.created_at)",
        lazy='dynamic',
    )

The result of accessing the relationship varies depending on the attribute lazy.

Herzig answered 12/7, 2022 at 15:37 Comment(2)
How about using sqlalchemy-stubs package?Tolidine
docs.sqlalchemy.org/en/14/orm/extensions/mypy.htmlClepsydra
A
7

For SQLAlchemy 2.0 it would be something like:

import uuid
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.dialects.postgresql import UUID

class Email(Model):
    __tablename__ = 'emails'

    name: Mapped[str] = mapped_column(String, nullable=False)
    sender: Mapped[str] = mapped_column(
        String, default=default_sender, nullable=False
    )
    subject: Mapped[str] = mapped_column(String, nullable=False)
    html: Mapped[str] = mapped_column(String, nullable=False)
    template_id: Mapped[uuid.UUID] = mapped_column(
        UUID(as_uuid=True),
        ForeignKey('templates.id'),
        index=True,
    )
Ate answered 1/6, 2023 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.