sqlalchemy: what is the difference between declaring the cascade within the foreign key vs relation?

Posted by steve on Stack Overflow See other posts from Stack Overflow or by steve
Published on 2010-05-18T02:24:23Z Indexed on 2010/05/18 2:30 UTC
Read the original article Hit count: 238

Filed under:

what is the difference between declaring the cascade within a foreign key vs relations?

class Contact(Base):
    __tablename__ = 'contacts'
    id = Column(Integer, primary_key=True)
    addresses = relation("Address", backref="contact")

class Address(Base):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    contact_id = Column(Integer, ForeignKey('contact.id', onupdate="CASCADE", ondelete="CASCADE")))

vs

class Contact(Base):
    __tablename__ = 'contacts'
    id = Column(Integer, primary_key=True)
    addresses = relation("Address", backref="contact", cascade="all, delete-orphan")

class Address(Base):
    __tablename__ = 'addresses'
    id = Column(Integer, primary_key=True)
    contact_id = Column(Integer, ForeignKey('contact.id'))

with the foreign key declaration, it seems like the cascade is enforced at the database level. how does the relations approach work? thanks!

© Stack Overflow or respective owner

Related posts about sqlalchemy