关于删除级联 - SQL Alchemy + Postgres

时间:2016-12-30 14:24:45

标签: postgresql sqlalchemy cascade

我正在尝试在父(Product)和子表(ProductVariant)之间设置删除级联,但它无法正常工作。我做错了什么?

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 个答案:

没有答案