from sqlalchemy import Column, Integer, String, Float, ForeignKey from sqlalchemy.orm import relationship from .database import Base class Location(Base): """ SQLAlchemy model for the 'locations' table. """ __tablename__ = "locations" id = Column(Integer, primary_key=True, index=True) name = Column(String, unique=False, index=True) address = Column(String, unique=False, index=True) latitude = Column(Float, unique=False, index=True) longitude = Column(Float, unique=False, index=True) is_favorite = Column(Boolean, unique=False, index=True) def __repr__(self): return f"" class Route(Base): """ SQLAlchemy model for the 'routes' table. """ __tablename__ = "routes" id = Column(Integer, primary_key=True, index=True) name = Column(String, index=True) # Start and Endpoints (One-to-Many relationship) start_location_id = Column(Integer, ForeignKey('locations.id')) end_location_id = Column(Integer, ForeignKey('locations.id')) start_location = relationship("Location", foreign_keys=[start_id]) end_location = relationship("Location", foreign_keys=[end_id]) # Relationship to get waypoints ordered waypoints = relationship("Waypoint", order_by="Waypoint.order", back_populates="route") def __repr__(self): return f"" # Association Table for Many-to-Many relationsjip (Routes <-> Waypoints) class Waypoint(Base): """ SQLAlchemy model for the 'waypoints' table. """ __tablename__ = 'waypoints' id = Column(Integer, primary_key=True) route_id = Column(Integer, ForeignKey('routes.id')) location_id = Column(Integer, ForeignKey('locations.id')) order = Column(Integer, nullable=False) route = relationship("Route", back_populates="waypoints") location = relationship("Location")