Cascades

Mappers support the concept of configurable cascade behavior on relationship() constructs. This refers to how operations performed on a “parent” object relative to a particular Session should be propagated to items referred to by that relationship (e.g. “child” objects), and is affected by the relationship.cascade option.

The default behavior of cascade is limited to cascades of the so-called save-update and merge settings. The typical “alternative” setting for cascade is to add the delete and delete-orphan options; these settings are appropriate for related objects which only exist as long as they are attached to their parent, and are otherwise deleted.

Cascade behavior is configured using the relationship.cascade option on relationship():

  1. class Order(Base):
  2. __tablename__ = 'order'
  3. items = relationship("Item", cascade="all, delete-orphan")
  4. customer = relationship("User", cascade="save-update")

To set cascades on a backref, the same flag can be used with the backref() function, which ultimately feeds its arguments back into relationship():

  1. class Item(Base):
  2. __tablename__ = 'item'
  3. order = relationship("Order",
  4. backref=backref("items", cascade="all, delete-orphan")
  5. )

The Origins of Cascade

SQLAlchemy’s notion of cascading behavior on relationships, as well as the options to configure them, are primarily derived from the similar feature in the Hibernate ORM; Hibernate refers to “cascade” in a few places such as in Example: Parent/Child. If cascades are confusing, we’ll refer to their conclusion, stating “The sections we have just covered can be a bit confusing. However, in practice, it all works out nicely.”

The default value of relationship.cascade is save-update, merge. The typical alternative setting for this parameter is either all or more commonly all, delete-orphan. The all symbol is a synonym for save-update, merge, refresh-expire, expunge, delete, and using it in conjunction with delete-orphan indicates that the child object should follow along with its parent in all cases, and be deleted once it is no longer associated with that parent.

The list of available values which can be specified for the relationship.cascade parameter are described in the following subsections.

save-update

save-update cascade indicates that when an object is placed into a Session via Session.add(), all the objects associated with it via this relationship() should also be added to that same Session. Suppose we have an object user1 with two related objects address1, address2:

  1. >>> user1 = User()
  2. >>> address1, address2 = Address(), Address()
  3. >>> user1.addresses = [address1, address2]

If we add user1 to a Session, it will also add address1, address2 implicitly:

  1. >>> sess = Session()
  2. >>> sess.add(user1)
  3. >>> address1 in sess
  4. True

save-update cascade also affects attribute operations for objects that are already present in a Session. If we add a third object, address3 to the user1.addresses collection, it becomes part of the state of that Session:

  1. >>> address3 = Address()
  2. >>> user1.append(address3)
  3. >>> address3 in sess
  4. >>> True

A save-update cascade can exhibit surprising behavior when removing an item from a collection or de-associating an object from a scalar attribute. In some cases, the orphaned objects may still be pulled into the ex-parent’s Session; this is so that the flush process may handle that related object appropriately. This case usually only arises if an object is removed from one Session and added to another:

  1. >>> user1 = sess1.query(User).filter_by(id=1).first()
  2. >>> address1 = user1.addresses[0]
  3. >>> sess1.close() # user1, address1 no longer associated with sess1
  4. >>> user1.addresses.remove(address1) # address1 no longer associated with user1
  5. >>> sess2 = Session()
  6. >>> sess2.add(user1) # ... but it still gets added to the new session,
  7. >>> address1 in sess2 # because it's still "pending" for flush
  8. True

The save-update cascade is on by default, and is typically taken for granted; it simplifies code by allowing a single call to Session.add() to register an entire structure of objects within that Session at once. While it can be disabled, there is usually not a need to do so.

One case where save-update cascade does sometimes get in the way is in that it takes place in both directions for bi-directional relationships, e.g. backrefs, meaning that the association of a child object with a particular parent can have the effect of the parent object being implicitly associated with that child object’s Session; this pattern, as well as how to modify its behavior using the relationship.cascade_backrefs flag, is discussed in the section Controlling Cascade on Backrefs.

delete

The delete cascade indicates that when a “parent” object is marked for deletion, its related “child” objects should also be marked for deletion. If for example we have a relationship User.addresses with delete cascade configured:

  1. class User(Base):
  2. # ...
  3. addresses = relationship("Address", cascade="all, delete")

If using the above mapping, we have a User object and two related Address objects:

  1. >>> user1 = sess.query(User).filter_by(id=1).first()
  2. >>> address1, address2 = user1.addresses

If we mark user1 for deletion, after the flush operation proceeds, address1 and address2 will also be deleted:

  1. >>> sess.delete(user1)
  2. >>> sess.commit()
  3. DELETE FROM address WHERE address.id = ?
  4. ((1,), (2,))
  5. DELETE FROM user WHERE user.id = ?
  6. (1,)
  7. COMMIT

Alternatively, if our User.addresses relationship does not have delete cascade, SQLAlchemy’s default behavior is to instead de-associate address1 and address2 from user1 by setting their foreign key reference to NULL. Using a mapping as follows:

  1. class User(Base):
  2. # ...
  3. addresses = relationship("Address")

Upon deletion of a parent User object, the rows in address are not deleted, but are instead de-associated:

  1. >>> sess.delete(user1)
  2. >>> sess.commit()
  3. UPDATE address SET user_id=? WHERE address.id = ?
  4. (None, 1)
  5. UPDATE address SET user_id=? WHERE address.id = ?
  6. (None, 2)
  7. DELETE FROM user WHERE user.id = ?
  8. (1,)
  9. COMMIT

delete cascade on one-to-many relationships is often combined with delete-orphan cascade, which will emit a DELETE for the related row if the “child” object is deassociated from the parent. The combination of delete and delete-orphan cascade covers both situations where SQLAlchemy has to decide between setting a foreign key column to NULL versus deleting the row entirely.

The feature by default works completely independently of database-configured FOREIGN KEY constraints that may themselves configure CASCADE behavior. In order to integrate more efficiently with this configuration, additional directives described at Using foreign key ON DELETE cascade with ORM relationships should be used.

See also

Using foreign key ON DELETE cascade with ORM relationships

Using delete cascade with many-to-many relationships

delete-orphan

Using delete cascade with many-to-many relationships

The cascade="all, delete" option works equally well with a many-to-many relationship, one that uses relationship.secondary to indicate an association table. When a parent object is deleted, and therefore de-associated with its related objects, the unit of work process will normally delete rows from the association table, but leave the related objects intact. When combined with cascade="all, delete", additional DELETE statements will take place for the child rows themselves.

The following example adapts that of Many To Many to illustrate the cascade="all, delete" setting on one side of the association:

  1. association_table = Table('association', Base.metadata,
  2. Column('left_id', Integer, ForeignKey('left.id')),
  3. Column('right_id', Integer, ForeignKey('right.id'))
  4. )
  5. class Parent(Base):
  6. __tablename__ = 'left'
  7. id = Column(Integer, primary_key=True)
  8. children = relationship(
  9. "Child",
  10. secondary=association_table,
  11. back_populates="parents",
  12. cascade="all, delete"
  13. )
  14. class Child(Base):
  15. __tablename__ = 'right'
  16. id = Column(Integer, primary_key=True)
  17. parents = relationship(
  18. "Parent",
  19. secondary=association_table,
  20. back_populates="children",
  21. )

Above, when a Parent object is marked for deletion using Session.delete(), the flush process will as usual delete the associated rows from the association table, however per cascade rules it will also delete all related Child rows.

Warning

If the above cascade="all, delete" setting were configured on both relationships, then the cascade action would continue cascading through all Parent and Child objects, loading each children and parents collection encountered and deleting everything that’s connected. It is typically not desireable for “delete” cascade to be configured bidirectionally.

See also

Deleting Rows from the Many to Many Table

Using foreign key ON DELETE with many-to-many relationships

Using foreign key ON DELETE cascade with ORM relationships

The behavior of SQLAlchemy’s “delete” cascade overlaps with the ON DELETE feature of a database FOREIGN KEY constraint. SQLAlchemy allows configuration of these schema-level DDL behaviors using the ForeignKey and ForeignKeyConstraint constructs; usage of these objects in conjunction with Table metadata is described at ON UPDATE and ON DELETE.

In order to use ON DELETE foreign key cascades in conjunction with relationship(), it’s important to note first and foremost that the relationship.cascade setting must still be configured to match the desired “delete” or “set null” behavior (using delete cascade or leaving it omitted), so that whether the ORM or the database level constraints will handle the task of actually modifying the data in the database, the ORM will still be able to appropriately track the state of locally present objects that may be affected.

There is then an additional option on relationship() which indicates the degree to which the ORM should try to run DELETE/UPDATE operations on related rows itself, vs. how much it should rely upon expecting the database-side FOREIGN KEY constraint cascade to handle the task; this is the relationship.passive_deletes parameter and it accepts options False (the default), True and "all".

The most typical example is that where child rows are to be deleted when parent rows are deleted, and that ON DELETE CASCADE is configured on the relevant FOREIGN KEY constraint as well:

  1. class Parent(Base):
  2. __tablename__ = 'parent'
  3. id = Column(Integer, primary_key=True)
  4. children = relationship(
  5. "Child", back_populates="parent",
  6. cascade="all, delete",
  7. passive_deletes=True
  8. )
  9. class Child(Base):
  10. __tablename__ = 'child'
  11. id = Column(Integer, primary_key=True)
  12. parent_id = Column(Integer, ForeignKey('parent.id', ondelete="CASCADE"))
  13. parent = relationship("Parent", back_populates="children")

The behavior of the above configuration when a parent row is deleted is as follows:

  1. The application calls session.delete(my_parent), where my_parent is an instance of Parent.

  2. When the Session next flushes changes to the database, all of the currently loaded items within the my_parent.children collection are deleted by the ORM, meaning a DELETE statement is emitted for each record.

  3. If the my_parent.children collection is unloaded, then no DELETE statements are emitted. If the relationship.passive_deletes flag were not set on this relationship(), then a SELECT statement for unloaded Child objects would have been emitted.

  4. A DELETE statement is then emitted for the my_parent row itself.

  5. The database-level ON DELETE CASCADE setting ensures that all rows in child which refer to the affected row in parent are also deleted.

  6. The Parent instance referred to by my_parent, as well as all instances of Child that were related to this object and were loaded (i.e. step 2 above took place), are de-associated from the Session.

Note

To use “ON DELETE CASCADE”, the underlying database engine must support FOREIGN KEY constraints and they must be enforcing:

Notes on Passive Deletes

It is important to note the differences between the ORM and the relational database’s notion of “cascade” as well as how they integrate:

  • A database level ON DELETE cascade is configured effectively on the many-to-one side of the relationship; that is, we configure it relative to the FOREIGN KEY constraint that is the “many” side of a relationship. At the ORM level, this direction is reversed. SQLAlchemy handles the deletion of “child” objects relative to a “parent” from the “parent” side, which means that delete and delete-orphan cascade are configured on the one-to-many side.

  • Database level foreign keys with no ON DELETE setting are often used to prevent a parent row from being removed, as it would necessarily leave an unhandled related row present. If this behavior is desired in a one-to-many relationship, SQLAlchemy’s default behavior of setting a foreign key to NULL can be caught in one of two ways:

    • The easiest and most common is just to set the foreign-key-holding column to NOT NULL at the database schema level. An attempt by SQLAlchemy to set the column to NULL will fail with a simple NOT NULL constraint exception.

    • The other, more special case way is to set the relationship.passive_deletes flag to the string "all". This has the effect of entirely disabling SQLAlchemy’s behavior of setting the foreign key column to NULL, and a DELETE will be emitted for the parent row without any affect on the child row, even if the child row is present in memory. This may be desirable in the case when database-level foreign key triggers, either special ON DELETE settings or otherwise, need to be activated in all cases when a parent row is deleted.

  • Database level ON DELETE cascade is generally much more efficient than relying upon the “cascade” delete feature of SQLAlchemy. The database can chain a series of cascade operations across many relationships at once; e.g. if row A is deleted, all the related rows in table B can be deleted, and all the C rows related to each of those B rows, and on and on, all within the scope of a single DELETE statement. SQLAlchemy on the other hand, in order to support the cascading delete operation fully, has to individually load each related collection in order to target all rows that then may have further related collections. That is, SQLAlchemy isn’t sophisticated enough to emit a DELETE for all those related rows at once within this context.

  • SQLAlchemy doesn’t need to be this sophisticated, as we instead provide smooth integration with the database’s own ON DELETE functionality, by using the relationship.passive_deletes option in conjunction with properly configured foreign key constraints. Under this behavior, SQLAlchemy only emits DELETE for those rows that are already locally present in the Session; for any collections that are unloaded, it leaves them to the database to handle, rather than emitting a SELECT for them. The section Using foreign key ON DELETE cascade with ORM relationships provides an example of this use.

  • While database-level ON DELETE functionality works only on the “many” side of a relationship, SQLAlchemy’s “delete” cascade has limited ability to operate in the reverse direction as well, meaning it can be configured on the “many” side to delete an object on the “one” side when the reference on the “many” side is deleted. However this can easily result in constraint violations if there are other objects referring to this “one” side from the “many”, so it typically is only useful when a relationship is in fact a “one to one”. The relationship.single_parent flag should be used to establish an in-Python assertion for this case.

Using foreign key ON DELETE with many-to-many relationships

As described at Using delete cascade with many-to-many relationships, “delete” cascade works for many-to-many relationships as well. To make use of ON DELETE CASCADE foreign keys in conjunction with many to many, FOREIGN KEY directives are configured on the association table. These directives can handle the task of automatically deleting from the association table, but cannot accommodate the automatic deletion of the related objects themselves.

In this case, the relationship.passive_deletes directive can save us some additional SELECT statements during a delete operation but there are still some collections that the ORM will continue to load, in order to locate affected child objects and handle them correctly.

Note

Hypothetical optimizations to this could include a single DELETE statement against all parent-associated rows of the association table at once, then use RETURNING to locate affected related child rows, however this is not currently part of the ORM unit of work implementation.

In this configuration, we configure ON DELETE CASCADE on both foreign key constraints of the association table. We configure cascade="all, delete" on the parent->child side of the relationship, and we can then configure passive_deletes=True on the other side of the bidirectional relationship as illustrated below:

  1. association_table = Table('association', Base.metadata,
  2. Column('left_id', Integer, ForeignKey('left.id', ondelete="CASCADE")),
  3. Column('right_id', Integer, ForeignKey('right.id', ondelete="CASCADE"))
  4. )
  5. class Parent(Base):
  6. __tablename__ = 'left'
  7. id = Column(Integer, primary_key=True)
  8. children = relationship(
  9. "Child",
  10. secondary=association_table,
  11. back_populates="parents",
  12. cascade="all, delete",
  13. )
  14. class Child(Base):
  15. __tablename__ = 'right'
  16. id = Column(Integer, primary_key=True)
  17. parents = relationship(
  18. "Parent",
  19. secondary=association_table,
  20. back_populates="children",
  21. passive_deletes=True
  22. )

Using the above configuration, the deletion of a Parent object proceeds as follows:

  1. A Parent object is marked for deletion using Session.delete().

  2. When the flush occurs, if the Parent.children collection is not loaded, the ORM will first emit a SELECT statement in order to load the Child objects that correspond to Parent.children.

  3. It will then then emit DELETE statements for the rows in association which correspond to that parent row.

  4. for each Child object affected by this immediate deletion, because passive_deletes=True is configured, the unit of work will not need to try to emit SELECT statements for each Child.parents collection as it is assumed the corresponding rows in association will be deleted.

  5. DELETE statements are then emitted for each Child object that was loaded from Parent.children.

delete-orphan

delete-orphan cascade adds behavior to the delete cascade, such that a child object will be marked for deletion when it is de-associated from the parent, not just when the parent is marked for deletion. This is a common feature when dealing with a related object that is “owned” by its parent, with a NOT NULL foreign key, so that removal of the item from the parent collection results in its deletion.

delete-orphan cascade implies that each child object can only have one parent at a time, and in the vast majority of cases is configured only on a one-to-many relationship. For the much less common case of setting it on a many-to-one or many-to-many relationship, the “many” side can be forced to allow only a single object at a time by configuring the relationship.single_parent argument, which establishes Python-side validation that ensures the object is associated with only one parent at a time, however this greatly limits the functionality of the “many” relationship and is usually not what’s desired.

See also

For relationship <relationship>, delete-orphan cascade is normally configured only on the “one” side of a one-to-many relationship, and not on the “many” side of a many-to-one or many-to-many relationship. - background on a common error scenario involving delete-orphan cascade.

merge

merge cascade indicates that the Session.merge() operation should be propagated from a parent that’s the subject of the Session.merge() call down to referred objects. This cascade is also on by default.

refresh-expire

refresh-expire is an uncommon option, indicating that the Session.expire() operation should be propagated from a parent down to referred objects. When using Session.refresh(), the referred objects are expired only, but not actually refreshed.

expunge

expunge cascade indicates that when the parent object is removed from the Session using Session.expunge(), the operation should be propagated down to referred objects.

Controlling Cascade on Backrefs

Note

This section applies to a behavior that is removed in SQLAlchemy 2.0. By setting the Session.future flag on a given Session, the 2.0 behavior will be achieved which is essentially that the relationship.cascade_backrefs flag is ignored. See the section cascade_backrefs behavior deprecated for removal in 2.0 for notes.

In 1.x style ORM usage, the save-update cascade by default takes place on attribute change events emitted from backrefs. This is probably a confusing statement more easily described through demonstration; it means that, given a mapping such as this:

  1. mapper_registry.map_imperatively(Order, order_table, properties={
  2. 'items' : relationship(Item, backref='order')
  3. })

If an Order is already in the session, and is assigned to the order attribute of an Item, the backref appends the Item to the items collection of that Order, resulting in the save-update cascade taking place:

  1. >>> o1 = Order()
  2. >>> session.add(o1)
  3. >>> o1 in session
  4. True
  5. >>> i1 = Item()
  6. >>> i1.order = o1
  7. >>> i1 in o1.items
  8. True
  9. >>> i1 in session
  10. True

This behavior can be disabled using the relationship.cascade_backrefs flag:

  1. mapper_registry.map_imperatively(Order, order_table, properties={
  2. 'items' : relationship(Item, backref='order', cascade_backrefs=False)
  3. })

So above, the assignment of i1.order = o1 will append i1 to the items collection of o1, but will not add i1 to the session. You can, of course, Session.add() i1 to the session at a later point. This option may be helpful for situations where an object needs to be kept out of a session until it’s construction is completed, but still needs to be given associations to objects which are already persistent in the target session.

Notes on Delete - Deleting Objects Referenced from Collections and Scalar Relationships

The ORM in general never modifies the contents of a collection or scalar relationship during the flush process. This means, if your class has a relationship() that refers to a collection of objects, or a reference to a single object such as many-to-one, the contents of this attribute will not be modified when the flush process occurs. Instead, it is expected that the Session would eventually be expired, either through the expire-on-commit behavior of Session.commit() or through explicit use of Session.expire(). At that point, any referenced object or collection associated with that Session will be cleared and will re-load itself upon next access.

A common confusion that arises regarding this behavior involves the use of the Session.delete() method. When Session.delete() is invoked upon an object and the Session is flushed, the row is deleted from the database. Rows that refer to the target row via foreign key, assuming they are tracked using a relationship() between the two mapped object types, will also see their foreign key attributes UPDATED to null, or if delete cascade is set up, the related rows will be deleted as well. However, even though rows related to the deleted object might be themselves modified as well, no changes occur to relationship-bound collections or object references on the objects involved in the operation within the scope of the flush itself. This means if the object was a member of a related collection, it will still be present on the Python side until that collection is expired. Similarly, if the object were referenced via many-to-one or one-to-one from another object, that reference will remain present on that object until the object is expired as well.

Below, we illustrate that after an Address object is marked for deletion, it’s still present in the collection associated with the parent User, even after a flush:

  1. >>> address = user.addresses[1]
  2. >>> session.delete(address)
  3. >>> session.flush()
  4. >>> address in user.addresses
  5. True

When the above session is committed, all attributes are expired. The next access of user.addresses will re-load the collection, revealing the desired state:

  1. >>> session.commit()
  2. >>> address in user.addresses
  3. False

There is a recipe for intercepting Session.delete() and invoking this expiration automatically; see ExpireRelationshipOnFKChange for this. However, the usual practice of deleting items within collections is to forego the usage of Session.delete() directly, and instead use cascade behavior to automatically invoke the deletion as a result of removing the object from the parent collection. The delete-orphan cascade accomplishes this, as illustrated in the example below:

  1. class User(Base):
  2. __tablename__ = 'user'
  3. # ...
  4. addresses = relationship(
  5. "Address", cascade="all, delete-orphan")
  6. # ...
  7. del user.addresses[1]
  8. session.flush()

Where above, upon removing the Address object from the User.addresses collection, the delete-orphan cascade has the effect of marking the Address object for deletion in the same way as passing it to Session.delete().

The delete-orphan cascade can also be applied to a many-to-one or one-to-one relationship, so that when an object is de-associated from its parent, it is also automatically marked for deletion. Using delete-orphan cascade on a many-to-one or one-to-one requires an additional flag relationship.single_parent which invokes an assertion that this related object is not to shared with any other parent simultaneously:

  1. class User(Base):
  2. # ...
  3. preference = relationship(
  4. "Preference", cascade="all, delete-orphan",
  5. single_parent=True)

Above, if a hypothetical Preference object is removed from a User, it will be deleted on flush:

  1. some_user.preference = None
  2. session.flush() # will delete the Preference object

See also

Cascades for detail on cascades.