Skip to content

fix: store translation_group in taxonomy parent_id to stop cross-locale parent leak - #1646

Merged
ascorbic merged 3 commits into
emdash-cms:mainfrom
mvanhorn:fix/1347-taxonomy-parent-group
Jun 29, 2026
Merged

ascorbic merged 3 commits into
emdash-cms:mainfrom
mvanhorn:fix/1347-taxonomy-parent-group

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

What does this PR do?

Hierarchical taxonomy terms encoded a term's parent as a locale-bound row id, but every other taxonomy translation relationship keys off the locale-agnostic translation group. Because of that mismatch, a child term translated into a second locale could end up pointing at a parent row from a different locale. The per-locale tree then quietly collapsed: the child rendered at the root instead of under its parent, with no error to signal anything was wrong. A best-effort patch in the create path only helped when the translated parent already existed, and it never repaired children that were created earlier.

The fix is to treat parentage the same way the rest of the taxonomy code already treats cross-locale identity, by storing the parent's translation group rather than one locale's row. A child now links to the parent concept, so translating that parent later automatically re-nests the children that were waiting on it, in whichever locales they live. Reads resolve a parent within the child's own locale, so an unfiltered list that mixes locales keeps each child under the right parent instead of colliding on the shared group. Validation now compares groups too, which means a translation can no longer be parented to its own group, and a forward-only migration rewrites the parent links that already exist so current trees render correctly after upgrade.

This is backwards compatible: the migration is additive and forward-only, and the stored value stays a real row id (the group's anchor), so the existing foreign key on the column still holds.

Closes #1347

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible admin UI strings wrapped for translation (n/a: server-side handlers and a migration only, no admin UI strings)
  • I have added a changeset (this changes a published package)
  • New features link to an approved Discussion (n/a: bug fix)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 4.8

Screenshots / test output

Lint, typecheck, and the taxonomy, migration, and seed suites pass (342 tests). New cases cover a child nesting under a parent that is translated after it, a mixed-locale list keeping each child under its own parent, the migration converting a legacy cross-locale link, and the rejection of invalid or self-referential parents.

AI was used for assistance.

mvanhorn and others added 3 commits June 29, 2026 01:01
…le parent leak

Taxonomy term hierarchy encoded parent_id as a locale-bound row id while
every other taxonomy i18n relationship keys off translation_group. A child
translated in another locale could end up parented to a row in a different
locale, silently flattening that locale's tree.

parent_id now persists the parent's translation_group (mirroring
content_taxonomies.taxonomy_id). Writes resolve the parent to its group,
findChildren resolves group + locale, and the handler/runtime buildTree link
children by group within the locale-filtered set. Migration 045 backfills
existing parent_id row ids to their translation_group. Translating a parent
now adopts its existing children in every locale instead of flattening them.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MQ7G74DcZPFPEgDJMTEcCR
…linking

Address review findings:
- Resolve parent writes to a translation_group only when its anchor row still
  exists, so create/update and the 045 backfill never write a dangling self-FK
  value when a parent's anchor translation was deleted.
- Key buildTree by (locale, group) in both the handler and runtime so an
  unfiltered, multi-locale term list nests each child under the parent in its
  own locale instead of colliding on the shared translation_group.
- Resolve exported term parents by translation_group in export-seed so a
  localized hierarchy round-trips instead of flattening translated children.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MQ7G74DcZPFPEgDJMTEcCR
Thread the prospective translation_group into validateParentTerm so creating a
translation whose parent is in the term's own group is rejected, instead of
being stored and then attaching the term to itself in the tree. Document the
accepted root-rendering for legacy rows whose parent anchor was deleted (the
migration leaves their locale-bound id rather than write a dangling FK).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01MQ7G74DcZPFPEgDJMTEcCR
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a19a8b9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 520 lines across 9 files. Large PRs are harder to review and more likely to be closed without review.

If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.

See CONTRIBUTING.md for contribution guidelines.

@pkg-pr-new

pkg-pr-new Bot commented Jun 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1646

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1646

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1646

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1646

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1646

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1646

emdash

npm i https://pkg.pr.new/emdash@1646

create-emdash

npm i https://pkg.pr.new/create-emdash@1646

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1646

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1646

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1646

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1646

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1646

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1646

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1646

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1646

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1646

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1646

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1646

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1646

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1646

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1646

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1646

commit: a19a8b9

@emdashbot emdashbot 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.

I reviewed PR #1646, which fixes cross-locale parent leakage in hierarchical taxonomies by storing the parent's translation_group (anchor id) in taxonomies.parent_id instead of a locale-bound row id.

Approach assessment: This is the right fix for #1347. It aligns parentage with every other taxonomy i18n relationship (content_taxonomies.taxonomy_id, _emdash_menu_items.reference_id) and makes the tree correct at read time without requiring the translated parent to exist when the child is created. The forward-only migration rewrites existing links to the anchor group, which keeps the self-FK valid. The design is additive and backwards-compatible for published rows because stored values remain real row ids (the anchors).

What I checked:

  • Read the diff and the full changed files (handlers, repository, migrations, runtime helpers, export-seed, tests).
  • Traced the create/update/read/delete paths, seed import/export, and existing test expectations.
  • Verified the migration SQL is dialect-neutral, idempotent, and preserves FK validity by only rewriting to live anchor ids.
  • Checked buildTree implementations in both api/handlers/taxonomies.ts and taxonomies/index.ts; the (locale, group) keying correctly prevents cross-locale misattachment in unfiltered lists.
  • Confirmed validateParentTerm now compares translation groups, so self-parent and cycle checks work correctly across translations.
  • Confirmed handleTermDelete now guards deletion when any locale's children reference the term's group.
  • Reviewed for AGENTS.md conventions: SQL uses Kysely's safe template (no raw interpolation), the changeset is present, no admin UI strings or Tailwind changes are involved, and the API envelope shape is preserved.

Headline conclusion: The implementation is sound and the tests cover the key scenarios: parent translated after child, mixed-locale unfiltered list, cross-locale self-parent rejection, foreign-taxonomy parent rejection, and migration backfill. I found no bugs, regressions, or convention violations.

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice! Solid fix, thanks

@ascorbic
ascorbic merged commit c962929 into emdash-cms:main Jun 29, 2026
47 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jun 29, 2026
ascorbic added a commit that referenced this pull request Jun 29, 2026
main is at 0.24.0 in package.json with the 0.24.0 changelog written, but
0.24.0 was never published to npm: the release run that should have
published it found these two changesets had raced onto main and prepared
0.24.1 instead, skipping the 0.24.0 publish.

Temporarily remove the two pending changesets so the next Release run sees
no changesets, makes `changeset version` a no-op, and falls through to
`changeset publish` to release the already-versioned 0.24.0 (npm + tag +
GitHub release + plugin tarballs).

These changesets document #1646 and #1644, which belong in 0.24.1. They
are restored in a follow-up PR immediately after 0.24.0 publishes, which
cuts 0.24.1 with their text and attribution intact.
ascorbic added a commit that referenced this pull request Jun 29, 2026
Restores the two changesets temporarily removed in #1651 to publish the
already-versioned 0.24.0. With 0.24.0 now on npm, these belong in 0.24.1.

Each carries pinned `pr:`/`commit:`/`author:` magic lines so
@changesets/changelog-github credits the original PRs and authors
(#1646 / @mvanhorn, #1644 / @masonjames) rather than this release PR.
Without the override it would attribute to the commit that re-added the
files (this PR), losing the contributors' credit.
@emdashbot emdashbot Bot mentioned this pull request Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/needs-review No maintainer or bot review yet size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Taxonomy term hierarchy uses locale-bound parent_id, causing cross-locale parent leak and silent tree flattening

2 participants