remove intermediate grocery table and add related_products feature
This commit is contained in:
@@ -17,6 +17,17 @@ shopping_event_products = Table(
|
||||
Column('price', Float, nullable=False) # Price of this product at the time of this shopping event
|
||||
)
|
||||
|
||||
# Association table for many-to-many self-referential relationship between related products
|
||||
related_products = Table(
|
||||
'related_products',
|
||||
Base.metadata,
|
||||
Column('id', Integer, primary_key=True, autoincrement=True), # Artificial primary key
|
||||
Column('product_id', Integer, ForeignKey('products.id'), nullable=False),
|
||||
Column('related_product_id', Integer, ForeignKey('products.id'), nullable=False),
|
||||
Column('relationship_type', String, nullable=True), # Optional: e.g., "size_variant", "brand_variant", "similar"
|
||||
Column('created_at', DateTime(timezone=True), server_default=func.now())
|
||||
)
|
||||
|
||||
class BrandInShop(Base):
|
||||
__tablename__ = "brands_in_shops"
|
||||
|
||||
@@ -51,27 +62,14 @@ class GroceryCategory(Base):
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
groceries = relationship("Grocery", back_populates="category")
|
||||
|
||||
class Grocery(Base):
|
||||
__tablename__ = "groceries"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False, index=True)
|
||||
category_id = Column(Integer, ForeignKey("grocery_categories.id"), nullable=False)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
category = relationship("GroceryCategory", back_populates="groceries")
|
||||
products = relationship("Product", back_populates="grocery")
|
||||
products = relationship("Product", back_populates="category")
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = "products"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, nullable=False, index=True)
|
||||
grocery_id = Column(Integer, ForeignKey("groceries.id"), nullable=False)
|
||||
category_id = Column(Integer, ForeignKey("grocery_categories.id"), nullable=False)
|
||||
brand_id = Column(Integer, ForeignKey("brands.id"), nullable=True)
|
||||
organic = Column(Boolean, default=False)
|
||||
weight = Column(Float, nullable=True) # in grams or kg
|
||||
@@ -80,9 +78,26 @@ class Product(Base):
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
grocery = relationship("Grocery", back_populates="products")
|
||||
category = relationship("GroceryCategory", back_populates="products")
|
||||
brand = relationship("Brand", back_populates="products")
|
||||
shopping_events = relationship("ShoppingEvent", secondary=shopping_event_products, back_populates="products")
|
||||
|
||||
# Self-referential many-to-many relationship for related products
|
||||
related_products = relationship(
|
||||
"Product",
|
||||
secondary=related_products,
|
||||
primaryjoin=id == related_products.c.product_id,
|
||||
secondaryjoin=id == related_products.c.related_product_id,
|
||||
back_populates="related_to_products"
|
||||
)
|
||||
|
||||
related_to_products = relationship(
|
||||
"Product",
|
||||
secondary=related_products,
|
||||
primaryjoin=id == related_products.c.related_product_id,
|
||||
secondaryjoin=id == related_products.c.product_id,
|
||||
back_populates="related_products"
|
||||
)
|
||||
|
||||
class Shop(Base):
|
||||
__tablename__ = "shops"
|
||||
|
||||
Reference in New Issue
Block a user