Repository navigation
fix: store translation_group in taxonomy parent_id to stop cross-locale parent leak - #1646
Conversation
…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 detectedLatest commit: a19a8b9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
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 |
Scope checkThis 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. |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
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
buildTreeimplementations in bothapi/handlers/taxonomies.tsandtaxonomies/index.ts; the(locale, group)keying correctly prevents cross-locale misattachment in unfiltered lists. - Confirmed
validateParentTermnow compares translation groups, so self-parent and cycle checks work correctly across translations. - Confirmed
handleTermDeletenow 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.
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.
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.
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
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runAI-generated code disclosure
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.