Skip to content

fix(outliner): do not revert many to one on undo - #13526

Merged
tiensonqin merged 4 commits into
masterfrom
logseq/undo-omit-many-to-one-28e8
Sep 30, 2026
Merged

tiensonqin merged 4 commits into
masterfrom
logseq/undo-omit-many-to-one-28e8

Conversation

@tiensonqin

@tiensonqin tiensonqin commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Related to logseq/db-test#1306 and the closed approach in #13480.

Reverting :many to :one is not supported, including on undo.

The previous inverse of :upsert-property restored the full prior schema, including :db/cardinality :db.cardinality/one. Applying that inverse threw Disallowed many to one conversion when values existed, so undo failed and cleared the history stack.

Change

  • Inverse of an existing-property :upsert-property omits :db/cardinality when restoring it would be many-to-one and the property already has values.
  • Delete-page restore of a property uses the same inverse schema helper.
  • update-datascript-schema keeps the current cardinality when the schema omits it, except types that cannot be :many (:checkbox, :datetime) which coerce to :one.
  • Replay of a schema that includes :one plus other fields skips the cardinality change when values exist, instead of throwing. Cardinality-only many-to-one with data still throws.
  • Unused properties can still restore :db.cardinality/one.

Tests

  • Undo/redo of one-to-many with a value, and with choices plus a value.
  • Construct test for the omitted-cardinality inverse.
  • Type change of an unused :many property to checkbox/datetime restores :one; change to number keeps :many.
  • Replayed schema with :one plus type keeps :many when values exist.
  • Cardinality-only many-to-one with existing data still throws.
Open in Web Open in Cursor 

Devin Review

devin-ai-integration[bot]

This comment was marked as resolved.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

Review — fix(outliner): do not revert many to one on undo

The core fix is correct: the recorded inverse now omits :db/cardinality exactly when restoring it would revert :many→:one while values exist, and update-datascript-schema preserving the current cardinality on omission is what lets the replayed inverse succeed. Verified end to end — the new tests pass and the traced path (derive at forward time → client_ops → apply-history-action! → upsert-property! → update-property → update-datascript-schema) holds.

Findings

[Important] Omitted cardinality now preserves :many into types that can't be :many

Location: deps/outliner/src/logseq/outliner/property.cljs — update-datascript-schema, the new else branch at ~line 260-264.

Issue: when a schema omits :db/cardinality, the property's current cardinality is now kept unconditionally. A :many property whose type is changed to :checkbox or :datetime (neither is in db-property-type/cardinality-property-types) keeps :db.cardinality/many, producing an invalid property configuration. Reachable from the UI: property-type-sub-pane (src/main/frontend/components/property/config.cljs ~L818) calls upsert-property! with {:logseq.property/type ...} and no cardinality for all 8 user-built-in-property-types, including :checkbox and :datetime. The :checkbox-allows-no-:many throw at upsert-property! (~L936) only inspects the schema, so the omission path bypasses it, and the "multiple values" toggle is hidden for those types so the user can't flip it back. Verified at runtime: type-changing an empty :many :default property to :checkbox/:datetime leaves :db.cardinality/many on the entity. Previously the implicit :one default made the same operation coerce to :one.

Impact: invalid :many checkbox/datetime properties, with no UI path back to :one; value schema becomes a set for a type that writes single values.

Suggestion: when new-type is not in cardinality-property-types, coerce to :db.cardinality/one (or throw) even when the schema omits cardinality.

[Important] The same throw path remains on build-inverse-delete-page

Location: deps/outliner/src/logseq/outliner/op/construct.cljc ~L972-975.

Issue: undoing a property-page delete rebuilds the property with (db-property/get-property-schema (into {} page)) verbatim — including :db.cardinality/one — instead of going through inverse-upsert-property-schema. If the property was recreated as :many and has values at undo time, replaying this inverse throws Disallowed many to one conversion, which undo_redo.cljs catches by calling clear-history! — the exact crash this PR fixes, on the neighboring path.

Suggestion: reuse inverse-upsert-property-schema db-before db-after for the property case here too.

[Minor] Values added between the toggle and the undo still throw

The omission decision is made at record time against db-after. If the property had no values when toggled, the inverse keeps :one; a value added afterwards by a transaction not in the local undo stack (e.g. an RTC-applied remote tx — remote ops don't cross clients, only :tx-data does) still hits the guard → throw + clear-history!. Rare interleaving, but the failure mode the PR removes still has this residual entry point. Could be addressed by stripping :db/cardinality at replay time when datoms exist, rather than only at record time — worth deciding deliberately.

[Question] #{:one :db.cardinality/one} in inverse-upsert-property-schema

get-property-schema only yields canonical :db.cardinality/*, so the :one shorthand arm is unreachable here — matches the existing many->one? set at property.cljs ~L317 so it's consistent, just dead defense. Same for the (or (:db/cardinality property) :db.cardinality/one) fallback — every property created via build-new-property already carries cardinality; fine to keep, flagging since the codebase leans no-defaults.

Verification

  • deps/outliner nbb suite on the PR head: 148 tests / 765 assertions, all pass (excluding two scratch prints I added then removed while verifying finding 1).
  • :test build unit tests in touched worker areas pass.
  • New tests cover the record-time omission, many→one-with-data rejection, and undo/redo with a value and with choices. Missing coverage: the invalid :many type-change above, and the delete-page undo path.

System additions

  • inverse-upsert-property-schema (construct.cljc) — Retain. Targeted and minimal.
  • Cardinality omission in the inverse :upsert-property — Retain.
  • Omission → keep-current semantics in update-datascript-schema — Retain with the type-eligibility guard from finding 1.

cursoragent and others added 4 commits September 30, 2026 17:32
Keep :db.cardinality/many when undoing a one-to-many toggle that already
has values. The inverse omits :db/cardinality so undo does not throw or
clear history.

Co-authored-by: Tienson Qin <[email protected]>
Closed-value properties still run a schema update on undo. Missing
:db/cardinality must not default to :one.

Co-authored-by: Tienson Qin <[email protected]>
Coerce types that cannot be :many back to :one. Skip an unsafe
cardinality restore when a replayed schema has other fields.

Co-authored-by: Tienson Qin <[email protected]>
@tiensonqin
tiensonqin force-pushed the logseq/undo-omit-many-to-one-28e8 branch from 67bc381 to bdd614f Compare September 30, 2026 09:32
@tiensonqin
tiensonqin merged commit 1f1589e into master Sep 30, 2026
11 of 14 checks passed
@tiensonqin
tiensonqin deleted the logseq/undo-omit-many-to-one-28e8 branch September 30, 2026 09:32

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Devin Review

Comment on lines +311 to +314
(if (and many->one? has-values?)
(if (seq (dissoc schema :db/cardinality))
(dissoc schema :db/cardinality)
(throw-disallowed-many-to-one!))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Multi-field cardinality change silently fails

With existing values, schema-for-update silently drops a requested :one conversion whenever the schema includes another field. The upsert succeeds but leaves the property at :many.

Learn more

An upsert can update a property schema directly, not just replay an undo operation. For a property with existing values and cardinality :many, a request containing :db/cardinality :one plus any other field reaches this branch. It drops the cardinality and completes normally, while a cardinality-only request raises an error. The caller cannot detect that the requested conversion failed.

Example: Given a populated :user.property/note with cardinality :many, (upsert-property! conn :user.property/note {:db/cardinality :one :logseq.property/type :default} {}) returns while the property remains :many; passing only {:db/cardinality :one} throws.

Recommended fix: Separate undo replay behavior from ordinary upsert-property! requests, or reject explicit many-to-one requests consistently. Preserve the inverse's ability to omit cardinality through inverse-upsert-property-schema without silently accepting direct conversion requests.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Sep 30, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
* fix(outliner): do not revert many to one on undo

Keep :db.cardinality/many when undoing a one-to-many toggle that already
has values. The inverse omits :db/cardinality so undo does not throw or
clear history.

Co-authored-by: Tienson Qin <[email protected]>

* fix(outliner): keep cardinality when schema omits it

Closed-value properties still run a schema update on undo. Missing
:db/cardinality must not default to :one.

Co-authored-by: Tienson Qin <[email protected]>

* fix(outliner): keep many-to-one undo from invalid types

Coerce types that cannot be :many back to :one. Skip an unsafe
cardinality restore when a replayed schema has other fields.

Co-authored-by: Tienson Qin <[email protected]>

* fix(test): balance parens in property cardinality tests

Co-authored-by: Tienson Qin <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 1, 2026
Keep :db.cardinality/many when undoing a one-to-many toggle that already
has values. The inverse omits :db/cardinality so undo does not throw or
clear history.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 1, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 4, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
* fix(outliner): do not revert many to one on undo

Keep :db.cardinality/many when undoing a one-to-many toggle that already
has values. The inverse omits :db/cardinality so undo does not throw or
clear history.

Co-authored-by: Tienson Qin <[email protected]>

* fix(outliner): keep cardinality when schema omits it

Closed-value properties still run a schema update on undo. Missing
:db/cardinality must not default to :one.

Co-authored-by: Tienson Qin <[email protected]>

* fix(outliner): keep many-to-one undo from invalid types

Coerce types that cannot be :many back to :one. Skip an unsafe
cardinality restore when a replayed schema has other fields.

Co-authored-by: Tienson Qin <[email protected]>

* fix(test): balance parens in property cardinality tests

Co-authored-by: Tienson Qin <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
Keep :db.cardinality/many when undoing a one-to-many toggle that already
has values. The inverse omits :db/cardinality so undo does not throw or
clear history.
devin-ai-integration Bot pushed a commit that referenced this pull request Oct 8, 2026
* fix(outliner): do not revert many to one on undo

Keep :db.cardinality/many when undoing a one-to-many toggle that already
has values. The inverse omits :db/cardinality so undo does not throw or
clear history.

Co-authored-by: Tienson Qin <[email protected]>

* fix(outliner): keep cardinality when schema omits it

Closed-value properties still run a schema update on undo. Missing
:db/cardinality must not default to :one.

Co-authored-by: Tienson Qin <[email protected]>

* fix(outliner): keep many-to-one undo from invalid types

Coerce types that cannot be :many back to :one. Skip an unsafe
cardinality restore when a replayed schema has other fields.

Co-authored-by: Tienson Qin <[email protected]>

* fix(test): balance parens in property cardinality tests

Co-authored-by: Tienson Qin <[email protected]>

---------

Co-authored-by: Cursor Agent <[email protected]>
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 12, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 27, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 29, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 29, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 30, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 30, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 31, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 31, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 1, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 2, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 3, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 3, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 4, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 11, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 11, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 12, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 12, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 13, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 14, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 15, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 16, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 17, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 17, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 18, 2026
logseq#13526's 2 tests check the inverse op of a one-to-many property change.
With the datom replay, undo on a local graph uses no inverse op, so the
tests run on a graph marked synced, where undo still replays the ops.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants