from datetime import datetime from sqlalchemy import (Boolean, Column, DateTime, Float, ForeignKey, Index, Integer, String, Text, UniqueConstraint) from sqlalchemy.orm import relationship # Import Base from the central database module instead of creating a new one from app.core.database import Base class Stock(Base): __tablename__ = "stock" id = Column(Integer, primary_key=True, index=True) gtin = Column( String, index=True, nullable=False ) # Foreign key relationship would be ideal location = Column(String, nullable=False, index=True) quantity = Column(Integer, nullable=False, default=0) reserved_quantity = Column(Integer, default=0) # For orders being processed shop_id = Column(Integer, ForeignKey("shops.id")) # Optional: shop-specific stock created_at = Column(DateTime, default=datetime.utcnow, nullable=False) updated_at = Column( DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False ) # Relationships shop = relationship("Shop") # Composite unique constraint to prevent duplicate GTIN-location combinations __table_args__ = ( UniqueConstraint("gtin", "location", name="uq_stock_gtin_location"), Index( "idx_stock_gtin_location", "gtin", "location" ), # Composite index for efficient queries ) def __repr__(self): return f""