On Delete Cascade - SQL Alchemy + Postgres

568 Views Asked by At

I am trying to setup a delete cascade between a parent (Product) and a child table (ProductVariant), but it is not working. What Am I doing wrong?

from sqlalchemy.orm import relationship, backref

Base = declarative_base()

class Product(Base):
    __tablename__ = 'product'
    id = Column(Integer, primary_key=True)
    title = Column(String)
    collection_id = Column(Integer, ForeignKey('collection.id'))


class ProductVariant(Base):
    __tablename__ = 'product_variant'
    id = Column(Integer, primary_key=True)
    title = Column(String)
    product_id = Column(Integer, ForeignKey(Product.id, ondelete='CASCADE'), nullable=False)
    product = relationship(Product, backref= backref("variants", cascade='delete'))

enter image description here

0

There are 0 best solutions below