Skip to content

Commit f353a6a

Browse files
docs: document Diagram iteration API in spec and whats-new
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 13554a6 commit f353a6a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/about/whats-new-22.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,25 @@ Session.drop(dry_run=True)
294294

295295
If a descendant table lives in a schema that hasn't been activated, the graph-driven delete won't know about it. When the final `DELETE` fails with a foreign key error, DataJoint catches it and produces an actionable error message identifying which schema needs to be activated — rather than the opaque crash of the prior implementation.
296296

297+
### Iteration API
298+
299+
Diagrams support Python's iteration protocol, yielding `FreeTable` objects in topological order:
300+
301+
```python
302+
# Forward iteration (parents first) — useful for export/inspection
303+
for ft in diagram:
304+
print(ft.full_table_name, len(ft))
305+
306+
# Reverse iteration (leaves first) — used by delete and drop
307+
for ft in reversed(diagram):
308+
ft.delete_quick()
309+
```
310+
311+
Each yielded `FreeTable` carries any cascade or restrict conditions that have been applied. `Table.delete()` and `Table.drop()` use `reversed(diagram)` internally, replacing the manual `topo_sort()` loops from prior implementations.
312+
297313
### Architecture
298314

299-
`Table.delete()` constructs a `Diagram` internally, calls `cascade()` to compute the affected subgraph, then executes the delete itself in reverse topological order. The Diagram is purely a graph computation and inspection tool — it computes the cascade and provides `preview()`, but all mutation logic (transactions, SQL execution, prompts) lives in `Table.delete()` and `Table.drop()`.
315+
`Table.delete()` constructs a `Diagram` internally, calls `cascade()` to compute the affected subgraph, then iterates `reversed(diagram)` to delete leaves first. The Diagram is purely a graph computation and inspection tool — it computes the cascade and provides `preview()` and iteration, but all mutation logic (transactions, SQL execution, prompts) lives in `Table.delete()` and `Table.drop()`.
300316

301317
### Advantages over Error-Driven Cascade
302318

src/reference/specs/diagram.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ export.preview() # only tables with matching rows
231231
export # visualize the export subgraph
232232
```
233233

234+
### Iteration
235+
236+
Diagrams support iteration in topological order:
237+
238+
| Method | Order | Use Case |
239+
|--------|-------|----------|
240+
| `for ft in diagram` | Parents first | Data export, inspection |
241+
| `for ft in reversed(diagram)` | Leaves first | Cascade delete, drop |
242+
243+
Each iteration yields a `FreeTable` with any cascade or restrict conditions applied. Alias nodes are skipped. Only nodes in the diagram's visible set (`nodes_to_show`) are yielded.
244+
245+
`Table.delete()` and `Table.drop()` use `reversed(diagram)` internally to execute mutations in safe dependency order.
246+
234247
### Restriction Propagation
235248

236249
When `cascade()` or `restrict()` propagates a restriction from a parent table to a child table, one of three rules applies depending on the foreign key relationship:

0 commit comments

Comments
 (0)