from typing import List from sqlalchemy import ForeignKey, String from sqlalchemy.orm import Mapped, mapped_column, relationship from backend.db.models.guid import GUID from .._model_base import SqlAlchemyBase, DeleteMixin class Houses(SqlAlchemyBase, DeleteMixin): __tablename__ = 'houses' id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate, index=True) icon: Mapped[str | None] = mapped_column(String, nullable=False) name: Mapped[str | None] = mapped_column(String, index=True, nullable=False) address: Mapped[str | None] = mapped_column(String, nullable=False) areas: Mapped[List["Areas"]] = relationship("Areas", back_populates="house", cascade="all, delete", passive_deletes=True) def __repr__(self): return f"{self.__class__.__name__}, name: {self.name}" class Areas(SqlAlchemyBase, DeleteMixin): __tablename__ = 'areas' id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate, index=True) house_id: Mapped[GUID] = mapped_column(GUID, ForeignKey('houses.id', ondelete='CASCADE'), nullable=False) name: Mapped[str | None] = mapped_column(String, index=True, nullable=False) desc: Mapped[str | None] = mapped_column(String, nullable=False) house: Mapped['Houses'] = relationship(back_populates="areas") def __repr__(self): return f"<{self.__class__.__name__} id={self.id} name={self.name} house_id={self.house_id}, desc={self.desc}>"