Files

53 lines
1.9 KiB
Python
Executable File

from datetime import datetime
from sqlalchemy import Column, Integer, String, Boolean, ForeignKey, DateTime, Date
from sqlalchemy.orm import relationship
from app.db.base import Base
class VehicleModel(Base):
"""Reference table for Make/Model (e.g., Toyota Corolla)"""
__tablename__ = "vehicle_models"
__table_args__ = {"schema": "ref"}
id = Column(Integer, primary_key=True, index=True)
make = Column(String, nullable=False)
model_name = Column(String, nullable=False)
category = Column(String) # sedan, truck, van
class Vehicle(Base):
"""The physical asset"""
__tablename__ = "vehicles"
__table_args__ = {"schema": "data"}
id = Column(Integer, primary_key=True, index=True)
model_id = Column(Integer, ForeignKey("ref.vehicle_models.id"))
vin = Column(String, unique=True, index=True, nullable=False)
license_plate = Column(String, unique=True, index=True, nullable=False)
production_year = Column(Integer)
status = Column(String, default="active") # active, maintenance, sold
created_at = Column(DateTime, default=datetime.utcnow)
# Relationships
history = relationship("VehicleHistory", back_populates="vehicle")
class VehicleHistory(Base):
"""
Defines who is driving what and when.
CRITICAL: This is the source of truth for 'Can User X see Vehicle Y?'
"""
__tablename__ = "vehicle_history"
__table_args__ = {"schema": "data"}
id = Column(Integer, primary_key=True, index=True)
vehicle_id = Column(Integer, ForeignKey("data.vehicles.id"), nullable=False)
user_id = Column(Integer, ForeignKey("data.users.id"), nullable=False)
role = Column(String, nullable=False) # owner, driver, fleet_manager
start_date = Column(Date, nullable=False)
end_date = Column(Date, nullable=True) # If NULL, the assignment is current
is_active = Column(Boolean, default=True)
vehicle = relationship("Vehicle", back_populates="history")