Files
romm/backend/models/platform.py
Michael Manganiello b99ededced misc: Migrate to SQLAlchemy declarative models
This change applies the guided migration process recommended by
SQLAlchemy [1], up to step 4, to have declarative ORM models that better
support Python typing.

The change was tested by running `alembic check`, which does not find
any schema changes.

Errors reported by `mypy` go down to 170, from the original 223 in the
current `master` commit.

[1] https://docs.sqlalchemy.org/en/20/changelog/whatsnew_20.html#migrating-an-existing-mapping
2024-06-25 23:03:20 -03:00

36 lines
1.2 KiB
Python

from typing import TYPE_CHECKING
from models.base import BaseModel
from models.rom import Rom
from sqlalchemy import String, func, select
from sqlalchemy.orm import Mapped, column_property, mapped_column, relationship
if TYPE_CHECKING:
from models.firmware import Firmware
class Platform(BaseModel):
__tablename__ = "platforms"
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
igdb_id: Mapped[int | None]
sgdb_id: Mapped[int | None]
moby_id: Mapped[int | None]
slug: Mapped[str] = mapped_column(String(length=50))
fs_slug: Mapped[str] = mapped_column(String(length=50))
name: Mapped[str | None] = mapped_column(String(length=400))
logo_path: Mapped[str | None] = mapped_column(String(length=1000), default="")
roms: Mapped[list["Rom"]] = relationship(back_populates="platform")
firmware: Mapped[list["Firmware"]] = relationship(
lazy="selectin", back_populates="platform"
)
# This runs a subquery to get the count of roms for the platform
rom_count = column_property(
select(func.count(Rom.id)).where(Rom.platform_id == id).scalar_subquery()
)
def __repr__(self) -> str:
return self.name