59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
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"<Location(name='{self.name}', address='{self.address}', latitude='{self.latitude}', longitude='{self.longitude}', is_favorite='{self.is_favorite}')>"
|
|
|
|
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"<Route(name='{self.name}', start='{self.start_location.name}', end='{self.end_location.name}')>"
|
|
|
|
# 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")
|