Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/34899-set-null-readonly-lookup-clear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@objectstack/objectql': patch
---

fix(objectql): deleting a record clears a read-only lookup that points at it

Clause-②: no
Metadata-protocol: none

Deleting a record makes the engine clear every optional lookup that points at it (`deleteBehavior: 'set_null'`, the default). That clear is an update of the referencing row under the caller's context. When the lookup was declared `readonly: true`, or locked by `readonlyWhen`, the read-only rule treated the clear as a value the caller had forged: it logged `... is read-only: the caller-supplied value was DROPPED and the update is being COMMITTED WITHOUT IT`, the delete answered 200, and every referencing row kept the deleted record's id.

The read-only rule now leaves that one key to the engine: the lookup the delete is clearing on that row, at the value the delete writes (`null`, or on a `multiple: true` lookup the remaining ids). No read-only line is logged for it, and the row's `beforeUpdate` hook sees the clear it is about to store. The exemption travels on an engine-internal channel that only the delete holds. A caller update that carries the operation-private `__referentialFieldClear` marker on its context is still stripped, still logged and still refused under `strictReadonlyWrites`. Any other read-only key in the same write is judged as before. `isSystem` is not granted.

The recycle-bin restore re-links such a lookup the same way. Its re-link also runs under the caller's context, so once the clear landed, the read-only rule dropped the re-link: after a restore the lookup stayed empty, and the restore still counted it in `relinked`. The re-link, and its undo when a restore fails partway, now use the same engine-internal channel. `relinked` counts only re-links that the row actually holds after the write.

Nothing to change in your metadata. Rows that already hold the id of a deleted record keep it: this fix does not rewrite them, so clear them with a system-context update.
20 changes: 16 additions & 4 deletions packages/objectql/src/engine-cascade-planning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ import { declaredHttpStatus } from '@objectstack/types';
import { ORGANIZATION_OBJECT, resolveTenantFieldName } from './tenancy/system-write-organization.js';
import { isFederatedUnprovisionedInjectedColumn } from './federated-object.js';
import {
CascadeBatchNarrowed, cascadeMemberKey, cascadeSetAdd, cascadeSetHas, declaredMultiValued, deleteWalkTargetOf,
statesDeleteSemantics,
CascadeBatchNarrowed, REFERENTIAL_CLEAR_OPTION, cascadeMemberKey, cascadeSetAdd, cascadeSetHas, declaredMultiValued,
deleteWalkTargetOf, statesDeleteSemantics,
} from './engine-top-level.js';
import type { ReferentialClearOption } from './engine-top-level.js';
import { isExternalDriverFloor } from './external-driver-floor.js';
import { forgetCollectedProbes, takeCollectedProbe } from './engine-delete-cascade.js';
import type { CascadeDeleteSet } from './engine-top-level.js';
Expand Down Expand Up @@ -1149,6 +1150,17 @@ export class CascadePlanningFacet {
object, id: String(id), referencingObject: childName, referencingId: String(depId), field: fieldName,
},
} as ExecutionContext;
// The read-only strips' exemption for THIS clear: the holder row
// and the lookup, on the update's module-private symbol channel
// (`REFERENTIAL_CLEAR_OPTION`), never on the context above. A
// `readonly` / `readonlyWhen` lookup is the normal shape for a
// stamp a system writes, and the static strip runs on
// `!isSystem` alone, so without it this clear was dropped as a
// forged caller write and every holder kept the deleted row's id.
// `update()` exempts exactly this key at exactly the value written
// below; every other key is judged as before.
const referentialClear: ReferentialClearOption = { object: childName, id: String(depId), field: fieldName };
const clearOptions = { context: referentialCtx, [REFERENTIAL_CLEAR_OPTION]: referentialClear };
// [ruling 6893104201 c1] The walk's first write: its hooks may
// write a row a later probe must see.
forgetCollectedProbes(cascadeSet);
Expand Down Expand Up @@ -1178,9 +1190,9 @@ export class CascadePlanningFacet {
// off-shape bare scalar normalized to the array spelling — is
// documented there.
const next = ObjectQL.remainderAfterMemberRemoval(dep?.[fieldName], id, cleared);
await this.update(childName, { id: depId, [fieldName]: next }, { context: referentialCtx } as any);
await this.update(childName, { id: depId, [fieldName]: next }, clearOptions as any);
} else {
await this.update(childName, { id: depId, [fieldName]: null }, { context: referentialCtx } as any);
await this.update(childName, { id: depId, [fieldName]: null }, clearOptions as any);
}
// [#30237, ADR-0148 D2] The reference a restore re-links while
// the field still lacks it: the live row, the field, and the id
Expand Down
4 changes: 3 additions & 1 deletion packages/objectql/src/engine-master-detail-related.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import { collectPredicateRelationships, referentialClearBinding } from './valida
// [#19911] The settlement's question "does judging the FK's own lock need the
// header it names?" — asked of the same root reader the strips use.
import { readonlyWhenFkJudgementReadsParent } from './validation/rule-validator.js';
import type { RelatedFieldBinding, RelatedRecordBinding } from './validation/rule-validator.js';
import type { ReferentialClearExemption, RelatedFieldBinding, RelatedRecordBinding } from './validation/rule-validator.js';
import { resolveMasterDetailRelation } from './master-detail.js';
import { isFederatedObject } from './federated-object.js';
import { masterIdOf, servedAsStored, staticReadonlyStoredView, undeclaredRelatedFields } from './engine.js';
Expand Down Expand Up @@ -138,6 +138,8 @@ function staticReadonlyStripTakes(
hookWrittenKeys: ReadonlySet<string> | undefined;
/** [#24822] The wall's flag, read once by `update()` — see {@link staticReadonlyStoredView}. */
wallJudgesOrganization?: boolean;
/** The walk's referential clear, read once by `update()` — see {@link staticReadonlyStoredView}. */
referentialClear?: ReferentialClearExemption;
},
): boolean {
if (!strip.runs) return false;
Expand Down
Loading
Loading