sqlalchemy: what is the difference between declaring the cascade within the foreign key vs relation?
- by steve
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!