@@ -18210,8 +18210,9 @@ export class ObjectQL implements IObjectQLEngine {
1821018210 * required lookup that escalation is decided per ROW after the dependents
1821118211 * probe (#9688): a row that keeps another member takes the member removal,
1821218212 * a row the removal would EMPTY keeps the refusal, and the refusal counts
18213- * only those rows. Only runs for single-id deletes — multi/predicate
18214- * deletes skip cascade (logged).
18213+ * only those rows. A by-id delete runs it for its one record; a predicate
18214+ * delete (`multi: true`) runs it for every row it matched ([#26871], see
18215+ * `delete()`'s predicate arm).
1821518216 *
1821618217 * [#22305] Two phases, so the walk knows its own set before it judges
1821718218 * anything:
@@ -19683,7 +19684,64 @@ export class ObjectQL implements IObjectQLEngine {
1968319684 // [#2982] The AST asserted present and already used for the
1968419685 // pre-phase row read above.
1968519686 // `deleteMany` presence is part of the ladder verdict resolved above.
19686- result = await driver.deleteMany!(object, opCtx.ast!, hookContext.input.options as any);
19687+ //
19688+ // [#26871] A predicate delete honours `deleteBehavior` as the
19689+ // by-id branch does: a `restrict` refuses, a `cascade` (a
19690+ // `master_detail` by default) deletes the dependents, a
19691+ // `set_null` clears them. This arm used to go straight to
19692+ // `deleteMany`, so a flow's `delete_record` with `multi: true`
19693+ // skipped every declared rule the same delete done record by
19694+ // record honours.
19695+ //
19696+ // `planCascadeAtomicity` is the by-id branch's own question:
19697+ // `'none'` means no relation of the delete family points at
19698+ // this object, so the walk could write nothing and the arm keeps
19699+ // its single `deleteMany`, no rows read and no transaction.
19700+ const plan = this.planCascadeAtomicity(object);
19701+ if (plan === 'none') {
19702+ result = await driver.deleteMany!(object, opCtx.ast!, hookContext.input.options as any);
19703+ } else {
19704+ // Otherwise: resolve the matched rows through the composed,
19705+ // row-scoped AST, run `cascadeDeleteRelations` for each, then
19706+ // delete exactly those rows by id. By id, not by re-running the
19707+ // predicate: the walk's own writes (a `set_null` on a
19708+ // self-reference) can move a row into or out of the predicate,
19709+ // and the rows deleted must be the rows walked. The ids were
19710+ // read under this caller's scope inside the same unit of work,
19711+ // as the by-id branch deletes its id after reading its
19712+ // pre-image under that scope. The unit of work is the by-id
19713+ // branch's, for the same reasons (#7413).
19714+ const runPredicateDelete = async (writeContext?: ExecutionContext): Promise<unknown> => {
19715+ const writeOptions = this.buildDriverOptions(object, writeContext, hookContext.input.options as any);
19716+ const matched = ((await driver.find(object, { ...opCtx.ast!, fields: ['id'] }, writeOptions)) ?? []) as Array<Record<string, unknown>>;
19717+ const matchedIds = matched.map((row) => row?.id).filter((v): v is string | number => v != null);
19718+ for (const matchedId of matchedIds) {
19719+ await this.cascadeDeleteRelations(object, matchedId, writeContext);
19720+ }
19721+ // The id set rides an `$in`; batched so one statement never
19722+ // outgrows a driver's bind-parameter limit.
19723+ const batchSize = 500;
19724+ let removed = 0;
19725+ for (let i = 0; i < matchedIds.length; i += batchSize) {
19726+ const batchAst: QueryAST = { object, where: { id: { $in: matchedIds.slice(i, i + batchSize) } } };
19727+ const count = await driver.deleteMany!(object, batchAst, writeOptions);
19728+ // A non-count answer goes back as the driver gave it, so
19729+ // the `isDeleteResultShape` check below refuses it.
19730+ if (typeof count !== 'number') return count;
19731+ removed += count;
19732+ }
19733+ return removed;
19734+ };
19735+ if (plan === 'atomic') {
19736+ result = await this.transaction(
19737+ async (trxCtx: any) => await runPredicateDelete(trxCtx as ExecutionContext),
19738+ opCtx.context,
19739+ );
19740+ } else {
19741+ this.warnCascadeNotAtomic(object);
19742+ result = await runPredicateDelete(opCtx.context);
19743+ }
19744+ }
1968719745 isPredicateWrite = true;
1968819746 }
1968919747
0 commit comments