its arguments back into :func:`~sqlalchemy.orm.relationship`::
class Item(Base):
- __tablename__ = 'item'
+ __tablename__ = "item"
- order = relationship("Order",
- backref=backref("items", cascade="all, delete-orphan")
- )
+ order = relationship(
+ "Order", backref=backref("items", cascade="all, delete-orphan")
+ )
.. sidebar:: The Origins of Cascade
bi-directionally to a series of ``Item`` objects via relationships
``Order.items`` and ``Item.order``::
- mapper_registry.map_imperatively(Order, order_table, properties={
- 'items' : relationship(Item, back_populates='order')
- })
+ mapper_registry.map_imperatively(
+ Order,
+ order_table,
+ properties={"items": relationship(Item, back_populates="order")},
+ )
- mapper_registry.map_imperatively(Item, item_table, properties={
- 'order' : relationship(Order, back_populates='items')
- })
+ mapper_registry.map_imperatively(
+ Item,
+ item_table,
+ properties={"order": relationship(Order, back_populates="items")},
+ )
If an ``Order`` is already associated with a :class:`_orm.Session`, and
an ``Item`` object is then created and appended to the ``Order.items``
illustrate the ``cascade="all, delete"`` setting on **one** side of the
association::
- association_table = Table('association', Base.metadata,
- Column('left_id', Integer, ForeignKey('left.id')),
- Column('right_id', Integer, ForeignKey('right.id'))
+ association_table = Table(
+ "association",
+ Base.metadata,
+ Column("left_id", Integer, ForeignKey("left.id")),
+ Column("right_id", Integer, ForeignKey("right.id")),
)
+
class Parent(Base):
- __tablename__ = 'left'
+ __tablename__ = "left"
id = Column(Integer, primary_key=True)
children = relationship(
"Child",
secondary=association_table,
back_populates="parents",
- cascade="all, delete"
+ cascade="all, delete",
)
+
class Child(Base):
- __tablename__ = 'right'
+ __tablename__ = "right"
id = Column(Integer, primary_key=True)
parents = relationship(
"Parent",
class Parent(Base):
- __tablename__ = 'parent'
+ __tablename__ = "parent"
id = Column(Integer, primary_key=True)
children = relationship(
- "Child", back_populates="parent",
+ "Child",
+ back_populates="parent",
cascade="all, delete",
- passive_deletes=True
+ passive_deletes=True,
)
+
class Child(Base):
- __tablename__ = 'child'
+ __tablename__ = "child"
id = Column(Integer, primary_key=True)
- parent_id = Column(Integer, ForeignKey('parent.id', ondelete="CASCADE"))
+ parent_id = Column(Integer, ForeignKey("parent.id", ondelete="CASCADE"))
parent = relationship("Parent", back_populates="children")
The behavior of the above configuration when a parent row is deleted
``passive_deletes=True`` on the **other** side of the bidirectional
relationship as illustrated below::
- association_table = Table('association', Base.metadata,
- Column('left_id', Integer, ForeignKey('left.id', ondelete="CASCADE")),
- Column('right_id', Integer, ForeignKey('right.id', ondelete="CASCADE"))
+ association_table = Table(
+ "association",
+ Base.metadata,
+ Column("left_id", Integer, ForeignKey("left.id", ondelete="CASCADE")),
+ Column("right_id", Integer, ForeignKey("right.id", ondelete="CASCADE")),
)
+
class Parent(Base):
- __tablename__ = 'left'
+ __tablename__ = "left"
id = Column(Integer, primary_key=True)
children = relationship(
"Child",
cascade="all, delete",
)
+
class Child(Base):
- __tablename__ = 'right'
+ __tablename__ = "right"
id = Column(Integer, primary_key=True)
parents = relationship(
"Parent",
secondary=association_table,
back_populates="children",
- passive_deletes=True
+ passive_deletes=True,
)
Using the above configuration, the deletion of a ``Parent`` object proceeds
illustrated in the example below::
class User(Base):
- __tablename__ = 'user'
+ __tablename__ = "user"
# ...
- addresses = relationship(
- "Address", cascade="all, delete-orphan")
+ addresses = relationship("Address", cascade="all, delete-orphan")
# ...
# ...
preference = relationship(
- "Preference", cascade="all, delete-orphan",
- single_parent=True)
-
+ "Preference", cascade="all, delete-orphan", single_parent=True
+ )
Above, if a hypothetical ``Preference`` object is removed from a ``User``,
it will be deleted on flush::