From 93047c8706a76be05e685e4c2e6f2bfc62cb436c Mon Sep 17 00:00:00 2001 From: Adrien Date: Mon, 8 May 2023 13:17:09 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Use=20declarative=20table?= =?UTF-8?q?=20configuration=20to=20define=20backend=20db=20tables?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/backend/models/line.py | 18 +++++++++--------- backend/backend/models/stop.py | 25 +++++++++++++------------ backend/backend/models/user.py | 24 +++++++++++++----------- 3 files changed, 35 insertions(+), 32 deletions(-) diff --git a/backend/backend/models/line.py b/backend/backend/models/line.py index b2fc4bf..4888e48 100644 --- a/backend/backend/models/line.py +++ b/backend/backend/models/line.py @@ -5,13 +5,11 @@ from typing import Iterable, Self, Sequence from sqlalchemy import ( BigInteger, Boolean, - Column, Enum, ForeignKey, Integer, select, String, - Table, ) from sqlalchemy.orm import Mapped, mapped_column, relationship, selectinload from sqlalchemy.sql.expression import tuple_ @@ -25,12 +23,14 @@ from ..idfm_interface.idfm_types import ( ) from .stop import _Stop -line_stop_association_table = Table( - "line_stop_association_table", - Base.metadata, - Column("line_id", ForeignKey("lines.id")), - Column("stop_id", ForeignKey("_stops.id")), -) + +class LineStopAssociations(Base): + + id = mapped_column(BigInteger, primary_key=True) + line_id = mapped_column(String, ForeignKey("lines.id")) + stop_id = mapped_column(BigInteger, ForeignKey("_stops.id")) + + __tablename__ = "line_stop_associations" class LinePicto(Base): @@ -83,7 +83,7 @@ class Line(Base): stops: Mapped[list[_Stop]] = relationship( "_Stop", - secondary=line_stop_association_table, + secondary="line_stop_associations", back_populates="lines", lazy="selectin", ) diff --git a/backend/backend/models/stop.py b/backend/backend/models/stop.py index dd7b87c..658869f 100644 --- a/backend/backend/models/stop.py +++ b/backend/backend/models/stop.py @@ -1,11 +1,10 @@ from __future__ import annotations from logging import getLogger -from typing import Annotated, Iterable, Sequence, TYPE_CHECKING +from typing import Iterable, Sequence, TYPE_CHECKING from sqlalchemy import ( BigInteger, - Column, Computed, desc, Enum, @@ -16,7 +15,6 @@ from sqlalchemy import ( JSON, select, String, - Table, ) from sqlalchemy.orm import ( mapped_column, @@ -37,12 +35,13 @@ if TYPE_CHECKING: logger = getLogger(__name__) -stop_area_stop_association_table = Table( - "stop_area_stop_association_table", - Base.metadata, - Column("stop_id", ForeignKey("_stops.id")), - Column("stop_area_id", ForeignKey("stop_areas.id")), -) +class StopAreaStopAssociations(Base): + + id = mapped_column(BigInteger, primary_key=True) + stop_id = mapped_column(BigInteger, ForeignKey("_stops.id")) + stop_area_id = mapped_column(BigInteger, ForeignKey("stop_areas.id")) + + __tablename__ = "stop_area_stop_associations" class _Stop(Base): @@ -64,12 +63,14 @@ class _Stop(Base): lines: Mapped[list[Line]] = relationship( "Line", - secondary="line_stop_association_table", + secondary="line_stop_associations", back_populates="stops", lazy="selectin", ) areas: Mapped[list["StopArea"]] = relationship( - "StopArea", secondary=stop_area_stop_association_table, back_populates="stops" + "StopArea", + secondary="stop_area_stop_associations", + back_populates="stops", ) connection_area_id: Mapped[int] = mapped_column( ForeignKey("connection_areas.id"), nullable=True @@ -143,7 +144,7 @@ class StopArea(_Stop): stops: Mapped[list["Stop"]] = relationship( "Stop", - secondary=stop_area_stop_association_table, + secondary="stop_area_stop_associations", back_populates="areas", lazy="selectin", ) diff --git a/backend/backend/models/user.py b/backend/backend/models/user.py index 5dedb8f..24a152c 100644 --- a/backend/backend/models/user.py +++ b/backend/backend/models/user.py @@ -1,25 +1,27 @@ -from sqlalchemy import Column, ForeignKey, String, Table +from sqlalchemy import BigInteger, ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column, relationship from ..db import Base, db from .stop import _Stop -user_last_stop_search_stops_associations_table = Table( - "user_last_stop_search_stops_associations_table", - Base.metadata, - Column("user_mxid", ForeignKey("user_last_stop_search_results.user_mxid")), - Column("stop_id", ForeignKey("_stops.id")), -) + +class UserLastStopSearchStopAssociations(Base): + id = mapped_column(BigInteger, primary_key=True) + user_mxid = mapped_column( + String, ForeignKey("user_last_stop_search_results.user_mxid") + ) + stop_id = mapped_column(BigInteger, ForeignKey("_stops.id")) + + __tablename__ = "user_last_stop_search_stop_associations" class UserLastStopSearchResults(Base): - db = db - __tablename__ = "user_last_stop_search_results" - user_mxid = mapped_column(String, primary_key=True) request_content = mapped_column(String, nullable=False) stops: Mapped[_Stop] = relationship( - _Stop, secondary=user_last_stop_search_stops_associations_table + _Stop, secondary="user_last_stop_search_stop_associations" ) + + __tablename__ = "user_last_stop_search_results"