40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
from sqlalchemy import Boolean, Column, DateTime, Float, ForeignKey, Integer, String, Text
|
|
from sqlalchemy.orm import relationship
|
|
from sqlalchemy.sql import func
|
|
|
|
from app.db.session import Base
|
|
|
|
|
|
class Category(Base):
|
|
__tablename__ = "categories"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, unique=True, index=True, nullable=False)
|
|
description = Column(Text)
|
|
|
|
# Relationships
|
|
products = relationship("Product", back_populates="category")
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
|
|
|
|
|
class Product(Base):
|
|
__tablename__ = "products"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
name = Column(String, index=True, nullable=False)
|
|
description = Column(Text)
|
|
price = Column(Float, nullable=False)
|
|
stock = Column(Integer, default=0)
|
|
image_url = Column(String)
|
|
is_active = Column(Boolean, default=True)
|
|
category_id = Column(Integer, ForeignKey("categories.id"))
|
|
|
|
# Relationships
|
|
category = relationship("Category", back_populates="products")
|
|
reviews = relationship("Review", back_populates="product", cascade="all, delete-orphan")
|
|
order_items = relationship("OrderItem", back_populates="product")
|
|
cart_items = relationship("CartItem", back_populates="product")
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|