Skip to content

fix: preserve user-defined shared join columns in change set - #12354

Merged
gioboa merged 2 commits into
typeorm:masterfrom
PreAgile:fix/12234-shared-join-column-omitted
May 10, 2026
Merged

gioboa merged 2 commits into
typeorm:masterfrom
PreAgile:fix/12234-shared-join-column-omitted

Conversation

@PreAgile

Copy link
Copy Markdown
Contributor

Summary

  • In computeDiffColumns, the relational-column short-circuit now only applies to auto-created join columns (where propertyName matches the owning relation's propertyName).
  • User-defined columns that also serve as join columns keep their explicit entity value in the change set, preventing them from being silently omitted when the relation is set to a scalar plain id.
  • Without the fix, updating a shared join column while a composite-FK relation is set drops the column from the UPDATE — causing NOT NULL constraint failed or the value reverting silently.

Fixes #12234

Test plan

  • Added test/github-issues/12234/ with 3 regression tests:
    • Scalar plain-id on composite FK → shared join column preserved on UPDATE
    • INSERT with only one composite-FK relation set → shared join column not omitted
    • Direct column value update → shared join column preserved
  • Verified the scalar test fails without the fix (NOT NULL constraint failed: post.tenantId)
  • pnpm run compile — pass
  • pnpm run typecheck — pass
  • prettier --check on changed files — pass
  • Existing persistence test suite (84 tests) — no regressions

In computeDiffColumns, the relational-column short-circuit now only
applies to auto-created join columns whose propertyName matches the
owning relation. User-defined columns that also serve as join columns
keep their explicit entity value in the change set, preventing them
from being silently omitted when the relation is set to a scalar plain id.

Closes typeorm#12234
@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Apr 16, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (2)

Grey Divider


Action required

1. Regression test added in github-issues 📘 Rule violation ⚙ Maintainability
Description
This PR adds new regression coverage only under test/github-issues/12234/ without any in-code
justification for not placing it in the functional test suite. This violates the preference to keep
issue fixes in test/functional to avoid accumulating per-issue tests.
Code

test/github-issues/12234/issue-12234.test.ts[R1-12]

+import { expect } from "chai"
+import type { DataSource } from "../../../src"
+import {
+    closeTestingConnections,
+    createTestingConnections,
+    reloadTestingDatabases,
+} from "../../utils/test-utils"
+import { Category } from "./entity/Category"
+import { Post } from "./entity/Post"
+
+describe("github issues > #12234 INSERT/UPDATE omits shared join column when relation is set to plain id", () => {
+    let dataSources: DataSource[]
Evidence
PR Compliance ID 3 requires issue-fix tests to be added/updated in test/functional, and flags
adding tests only under test/github-issues as a failure case without a clear reason. The PR
introduces the new test file under test/github-issues/12234/.

Rule 3: Prefer functional tests over per-issue tests
test/github-issues/12234/issue-12234.test.ts[1-12]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new regression tests are added under `test/github-issues/12234/` without a clear reason, but compliance requires issue-fix tests to live in `test/functional`.
## Issue Context
Keeping issue regressions in the functional suite reduces long-term maintenance overhead and avoids per-issue test sprawl.
## Fix Focus Areas
- test/github-issues/12234/issue-12234.test.ts[1-161]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Persistence flag ignored 🐞 Bug ≡ Correctness
Description
computeDiffColumns can now treat user-defined join/FK columns as changed based on the relation
property value, even when that relation has persistence: false, causing unintended FK updates on
save. This bypasses computeDiffRelationalColumns’ persistenceEnabled guard and contradicts the
documented persistenceEnabled behavior.
Code

src/persistence/SubjectChangedColumnsComputer.ts[R85-91]

+                if (
+                    column.propertyName ===
+                    column.relationMetadata?.propertyName
+                ) {
                   const value = column.relationMetadata.getEntityValue(
                       subject.entity!,
                   )
Evidence
computeDiffRelationalColumns explicitly skips relations with persistenceEnabled === false, which
is documented to prevent relation changes from being reflected in the DB. After this PR, the
short-circuit in computeDiffColumns no longer applies to typical user-defined join/FK columns
(e.g., userId, tenantId) because their column.propertyName does not equal the owning
relation’s propertyName; as a result, computeDiffColumns can produce a column changeMap for a
join column whose value is *derived from the relation object* via ColumnMetadata.getEntityValue,
thereby persisting the relation change through FK columns even though the relation itself is
skipped.

src/persistence/SubjectChangedColumnsComputer.ts[38-51]
src/persistence/SubjectChangedColumnsComputer.ts[70-93]
src/persistence/SubjectChangedColumnsComputer.ts[240-254]
src/metadata/RelationMetadata.ts[107-113]
src/metadata-builder/RelationJoinColumnBuilder.ts[225-275]
src/metadata/ColumnMetadata.ts[810-838]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new short-circuit in `computeDiffColumns` no longer prevents user-defined join/FK columns from being diffed when a relation is set. Because `ColumnMetadata.getEntityValue` can derive FK values from the relation object, this can cause join columns to be updated even when the owning relation has `persistence: false` (i.e., `persistenceEnabled === false`), effectively persisting a relation change that should be ignored.
### Issue Context
- `computeDiffRelationalColumns` returns early when `relation.persistenceEnabled === false`, so relation changes are intentionally skipped.
- Join columns are annotated with `relationMetadata`/`referencedColumn`, and `getEntityValue` will pull FK values from the relation object.
### Fix Focus Areas
- src/persistence/SubjectChangedColumnsComputer.ts[70-93]
- src/persistence/SubjectChangedColumnsComputer.ts[240-254]
- src/metadata/ColumnMetadata.ts[810-838]
### Suggested fix direction
Adjust the `computeDiffColumns` relation short-circuit so that when `column.relationMetadata.persistenceEnabled === false` and the join-column value is being derived from a relation object (vs coming from an explicit scalar value on the FK property), the column diff is skipped (preserving the `persistence: false` contract) while still allowing explicitly-set FK column values (including shared join columns) to be included in the change set.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

3. Uses as any cast 📘 Rule violation ⚙ Maintainability
Description
The new test assigns a scalar id to a relation property using an explicit as any cast, which
bypasses type safety. This violates the requirement to avoid any casts used to silence type
errors.
Code

test/github-issues/12234/issue-12234.test.ts[R68-71]

+                loaded.tenantId = 2
+                loaded.mainCategoryId = 20
+                loaded.mainCategory = 20 as any // scalar plain id
+                await manager.save(Post, loaded)
Evidence
PR Compliance ID 4 explicitly lists any-casts to bypass type issues as failure criteria. The added
test line loaded.mainCategory = 20 as any is an any cast used to assign an incompatible type.

Rule 4: Remove AI-generated noise
test/github-issues/12234/issue-12234.test.ts[68-71]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The test uses `as any` to bypass TypeScript when assigning a scalar plain id to a relation property.
## Issue Context
The test can still cover the scalar-id scenario without introducing `any` casts by using an explicit TypeScript expectation (e.g., `// @ts-expect-error`) on the assignment line.
## Fix Focus Areas
- test/github-issues/12234/issue-12234.test.ts[68-71]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Previous review results

Review updated until commit 1f69a43

Results up to commit bc9ee09


Details

🐞 Bugs (1)   📘 Rule violations (2)   📎 Requirement gaps (0)
🐞\ ≡ Correctness (1)
📘\ ⚙ Maintainability (2)

Action required
1. Regression test added in github-issues 📘 ⚙
Description
This PR adds new regression coverage only under test/github-issues/12234/ without any in-code
justification for not placing it in the functional test suite. This violates the preference to keep
issue fixes in test/functional to avoid accumulating per-issue tests.
Code

test/github-issues/12234/issue-12234.test.ts[R1-12]

+import { expect } from "chai"
+import type { DataSource } from "../../../src"
+import {
+    closeTestingConnections,
+    createTestingConnections,
+    reloadTestingDatabases,
+} from "../../utils/test-utils"
+import { Category } from "./entity/Category"
+import { Post } from "./entity/Post"
+
+describe("github issues > #12234 INSERT/UPDATE omits shared join column when relation is set to plain id", () => {
+    let dataSources: DataSource[]
Evidence
PR Compliance ID 3 requires issue-fix tests to be added/updated in test/functional, and flags
adding tests only under test/github-issues as a failure case without a clear reason. The PR
introduces the new test file under test/github-issues/12234/.

Rule 3: Prefer functional tests over per-issue tests
test/github-issues/12234/issue-12234.test.ts[1-12]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new regression tests are added under `test/github-issues/12234/` without a clear reason, but compliance requires issue-fix tests to live in `test/functional`.

## Issue Context
Keeping issue regressions in the functional suite reduces long-term maintenance overhead and avoids per-issue test sprawl.

## Fix Focus Areas
- test/github-issues/12234/issue-12234.test.ts[1-161]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Persistence flag ignored 🐞 ≡
Description
computeDiffColumns can now treat user-defined join/FK columns as changed based on the relation
property value, even when that relation has persistence: false, causing unintended FK updates on
save. This bypasses computeDiffRelationalColumns’ persistenceEnabled guard and contradicts the
documented persistenceEnabled behavior.
Code

src/persistence/SubjectChangedColumnsComputer.ts[R85-91]

+                if (
+                    column.propertyName ===
+                    column.relationMetadata?.propertyName
+                ) {
                    const value = column.relationMetadata.getEntityValue(
                        subject.entity!,
                    )
Evidence
computeDiffRelationalColumns explicitly skips relations with persistenceEnabled === false, which
is documented to prevent relation changes from being reflected in the DB. After this PR, the
short-circuit in computeDiffColumns no longer applies to typical user-defined join/FK columns
(e.g., userId, tenantId) because their column.propertyName does not equal the owning
relation’s propertyName; as a result, computeDiffColumns can produce a column changeMap for a
join column whose value is *derived from the relation object* via ColumnMetadata.getEntityValue,
thereby persisting the relation change through FK columns even though the relation itself is
skipped.

src/persistence/SubjectChangedColumnsComputer.ts[38-51]
src/persistence/SubjectChangedColumnsComputer.ts[70-93]
src/persistence/SubjectChangedColumnsComputer.ts[240-254]
src/metadata/RelationMetadata.ts[107-113]
src/metadata-builder/RelationJoinColumnBuilder.ts[225-275]
src/metadata/ColumnMetadata.ts[810-838]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
The new short-circuit in `computeDiffColumns` no longer prevents user-defined join/FK columns from being diffed when a relation is set. Because `ColumnMetadata.getEntityValue` can derive FK values from the relation object, this can cause join columns to be updated even when the owning relation has `persistence: false` (i.e., `persistenceEnabled === false`), effectively persisting a relation change that should be ignored.

### Issue Context
- `computeDiffRelationalColumns` returns early when `relation.persistenceEnabled === false`, so relation changes are intentionally skipped.
- Join columns are annotated with `relationMetadata`/`referencedColumn`, and `getEntityValue` will pull FK values from the relation object.

### Fix Focus Areas
- src/persistence/SubjectChangedColumnsComputer.ts[70-93]
- src/persistence/SubjectChangedColumnsComputer.ts[240-254]
- src/metadata/ColumnMetadata.ts[810-838]

### Suggested fix direction
Adjust the `computeDiffColumns` relation short-circuit so that when `column.relationMetadata.persistenceEnabled === false` and the join-column value is being derived from a relation object (vs coming from an explicit scalar value on the FK property), the column diff is skipped (preserving the `persistence: false` contract) while still allowing explicitly-set FK column values (including shared join columns) to be included in the change set.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended
3. Uses as any cast 📘 ⚙
Description
The new test assigns a scalar id to a relation property using an explicit as any cast, which
bypasses type safety. This violates the requirement to avoid any casts used to silence type
errors.
Code

test/github-issues/12234/issue-12234.test.ts[R68-71]

+                loaded.tenantId = 2
+                loaded.mainCategoryId = 20
+                loaded.mainCategory = 20 as any // scalar plain id
+                await manager.save(Post, loaded)
Evidence
PR Compliance ID 4 explicitly lists any-casts to bypass type issues as failure criteria. The added
test line loaded.mainCategory = 20 as any is an any cast used to assign an incompatible type.

Rule 4: Remove AI-generated noise
test/github-issues/12234/issue-12234.test.ts[68-71]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The test uses `as any` to bypass TypeScript when assigning a scalar plain id to a relation property.

## Issue Context
The test can still cover the scalar-id scenario without introducing `any` casts by using an explicit TypeScript expectation (e.g., `// @ts-expect-error`) on the assignment line.

## Fix Focus Areas
- test/github-issues/12234/issue-12234.test.ts[68-71]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


ⓘ The new review experience is currently in Beta. Learn more

Qodo Logo

@pkg-pr-new

pkg-pr-new Bot commented May 10, 2026

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12354
    

commit: 1f69a43

@gioboa
gioboa requested review from Cprakhar and alumni May 10, 2026 13:39
@alumni alumni added the backport This fix should be backported to a previous version label May 10, 2026
@gioboa
gioboa enabled auto-merge (squash) May 10, 2026 13:48

@gioboa gioboa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @PreAgile for your help 🤗

@gioboa
gioboa merged commit ef34796 into typeorm:master May 10, 2026
46 checks passed
@github-actions github-actions Bot added this to the 1.0 milestone May 10, 2026
alumni added a commit that referenced this pull request May 16, 2026
alumni added a commit that referenced this pull request May 18, 2026
@alumni alumni removed the backport This fix should be backported to a previous version label May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

INSERT/UPDATE omits shared join column when relation is set to plain id (0.3.28 regression)

4 participants