Skip to content

delete() ignores a dj.Top restriction and deletes every matching row #1589

Closed

Description

@dimitri-yatsenko

delete() and delete_quick() ignore a dj.Top restriction and delete every row that matches the expression's other conditions.

Item.insert([(i,) for i in range(5)])
(Item & dj.Top(2, order_by="item_id")).delete(prompt=False)
len(Item())   # 0 — expected 3

dj.Top is an ORDER BY ... LIMIT, kept in the expression's _top, not in its WHERE clause. Diagram.cascade builds the seed from table_expr.restriction only, and delete_quick uses where_clause() only, so both drop the Top. With no other condition, the whole table is deleted, along with its descendants. Diagram.trace has the same seed problem.

Workaround until the fix ships: restrict by the keys instead, (Item & (Item & dj.Top(2, order_by="item_id")).keys()).delete(). The delete prompt also shows the row counts before anything is deleted.

Fix: a seed restricted by Top is replaced by the primary keys it selects, in cascade, trace and delete_quick.

Activity

  1. added this to the v2.3.5 milestone on Oct 17, 2026
  2. added
    bugIndicates an unexpected problem or unintended behavior
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions