from sqlalchemy import Column, String, Float, Integer, ForeignKey, UniqueConstraint from sqlalchemy.orm import relationship from app.models.base_model import BaseModel class Cart(BaseModel): """Model for shopping cart.""" __tablename__ = "carts" user_id = Column(String(255), nullable=False, index=True) is_active = Column(Integer, default=1, nullable=False) items = relationship("CartItem", back_populates="cart", cascade="all, delete-orphan") class CartItem(BaseModel): """Model for items in shopping cart.""" __tablename__ = "cart_items" __table_args__ = ( UniqueConstraint("cart_id", "product_id", name="uix_cart_product"), ) cart_id = Column(Integer, ForeignKey("carts.id", ondelete="CASCADE"), nullable=False) product_id = Column(Integer, ForeignKey("products.id", ondelete="CASCADE"), nullable=False) quantity = Column(Integer, nullable=False, default=1) unit_price = Column(Float, nullable=False) # Stores the price at the time of adding to cart cart = relationship("Cart", back_populates="items") product = relationship("Product")