Skip to content

test: verify column names when using referencedColumnName in composite relations - #11883

Merged
gioboa merged 2 commits into
typeorm:masterfrom
Cprakhar:test/test-for-issue-7242
Jan 14, 2026
Merged

gioboa merged 2 commits into
typeorm:masterfrom
Cprakhar:test/test-for-issue-7242

Conversation

@Cprakhar

@Cprakhar Cprakhar commented Dec 30, 2025 •

Copy link
Copy Markdown
Collaborator

Close #7242
Added a test that shows that issue #7242 is resolved.

@Cprakhar
Cprakhar marked this pull request as ready for review December 30, 2025 11:35
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

7242 - PR Code Verified

Compliant requirements:

  • Reproduce the issue with entities having both simple and composite relations
  • Test entities match the structure described in the issue (Game, Participant, Match)
  • Verify generated SQL query uses correct column names in JOIN conditions
  • Test includes both leftJoinAndSelect for game and participant relations

Requires further human verification:

  • Verify the test actually fails before the fix and passes after (to confirm it properly tests the bug)
  • Confirm the test runs specifically against PostgreSQL driver as mentioned in the issue
  • Validate that the SQL assertion correctly identifies the bug scenario
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Test Assertion Logic

The test uses an OR condition to check for correct SQL in both MySQL and PostgreSQL formats. However, this approach may pass even if one database generates incorrect SQL while the other is correct. Consider splitting into database-specific test cases or ensuring both formats are validated when applicable.

const hasCorrectJoin =
    sql.includes(
        "LEFT JOIN `game` `game` ON `game`.`id`=`match`.`game_id`  LEFT JOIN `participant` `participant` ON `participant`.`user_id`=`match`.`user_id` AND `participant`.`game_id`=`match`.`game_id`",
    ) ||
    sql.includes(
        `LEFT JOIN "game" "game" ON "game"."id"="match"."game_id"  LEFT JOIN "participant" "participant" ON "participant"."user_id"="match"."user_id" AND "participant"."game_id"="match"."game_id"`,
    )
expect(hasCorrectJoin).to.be.true
Order Difference

The order of JoinColumn array elements differs from the original issue. Original has game_id first, then user_id, but test has user_id first, then game_id. While this may not affect the bug reproduction, it's worth verifying this doesn't impact the test's ability to catch the specific caching issue mentioned.

@JoinColumn([
    { name: "user_id", referencedColumnName: "userId" },
    { name: "game_id", referencedColumnName: "gameId" },
])

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Dec 30, 2025 •

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to c7b6d50

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix incorrect ManyToOne relation syntax

Correct the @ManyToOne relation syntax by removing the second parameter, as it
incorrectly references a field instead of an inverse relation property.

test/functional/metadata-builder/relation-join-column-builder/entity/Match.ts [16-18]

 @JoinColumn({ name: "game_id", referencedColumnName: "id" })
-@ManyToOne(() => Game, (game) => game.id, { eager: true })
+@ManyToOne(() => Game, { eager: true })
 game: Game
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies that using (game) => game.id as the inverse relation property in @ManyToOne is unconventional and against TypeORM's documented usage, improving code clarity and maintainability.

Low
  • More

Previous suggestions

Suggestions up to commit cf6aea9
CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix incorrect ManyToOne relation callback

Remove the incorrect inverse relation callback (game) => game.id from the
@ManyToOne decorator on the game property, as it is invalid.

test/functional/metadata-builder/relation-join-column-builder/entity/Match.ts [16-18]

 @JoinColumn({ name: "game_id", referencedColumnName: "id" })
-@ManyToOne(() => Game, (game) => game.id, { eager: true })
+@ManyToOne(() => Game, { eager: true })
 game: Game
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies that the inverse relation callback (game) => game.id in the @ManyToOne decorator is invalid, as it points to a primary key instead of a relation property. This creates incorrect entity metadata.

Low
General
Normalize whitespace in SQL assertion

Improve the SQL assertion by normalizing whitespace before checking for string
inclusion, making the test less fragile to formatting changes.

test/functional/metadata-builder/relation-join-column-builder/relation-join-column-builder.test.ts [129-135]

+const normalizedSql = sql.replace(/\s+/g, ' ').trim()
 const hasCorrectJoin =
-    sql.includes(
-        "LEFT JOIN `game` `game` ON `game`.`id`=`match`.`game_id`  LEFT JOIN `participant` `participant` ON `participant`.`user_id`=`match`.`user_id` AND `participant`.`game_id`=`match`.`game_id`",
+    normalizedSql.includes(
+        'LEFT JOIN `game` `game` ON `game`.`id`=`match`.`game_id` LEFT JOIN `participant` `participant` ON `participant`.`user_id`=`match`.`user_id` AND `participant`.`game_id`=`match`.`game_id`',
     ) ||
-    sql.includes(
-        `LEFT JOIN "game" "game" ON "game"."id"="match"."game_id"  LEFT JOIN "participant" "participant" ON "participant"."user_id"="match"."user_id" AND "participant"."game_id"="match"."game_id"`,
+    normalizedSql.includes(
+        'LEFT JOIN "game" "game" ON "game"."id"="match"."game_id" LEFT JOIN "participant" "participant" ON "participant"."user_id"="match"."user_id" AND "participant"."game_id"="match"."game_id"',
     )
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly points out that the test's SQL string matching is brittle due to its dependency on specific whitespace. Normalizing whitespace would make the test more robust against future formatting changes.

Low
Suggestions
CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix incorrect ManyToOne relation callback

Correct the @ManyToOne relation on the game property by removing the invalid
(game) => game.id callback, as it incorrectly specifies the inverse side of the
relation.

test/functional/metadata-builder/relation-join-column-builder/entity/Match.ts [16-18]

 @JoinColumn({ name: "game_id", referencedColumnName: "id" })
-@ManyToOne(() => Game, (game) => game.id, { eager: true })
+@ManyToOne(() => Game, { eager: true })
 game: Game
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies that the (game) => game.id callback in the @ManyToOne decorator is semantically incorrect for defining an inverse relation, improving code correctness and adherence to TypeORM conventions.

Low

@Cprakhar

Copy link
Copy Markdown
Collaborator Author

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

7242 - PR Code Verified

Compliant requirements:

  • Reproduce the issue with entities having both simple and composite relations
  • Test entities match the structure described in the issue (Game, Participant, Match)
  • Verify generated SQL query uses correct column names in JOIN conditions
  • Test includes both leftJoinAndSelect for game and participant relations

Requires further human verification:

  • Verify the test actually fails before the fix and passes after (to confirm it properly tests the bug)
  • Confirm the test runs specifically against PostgreSQL driver as mentioned in the issue
  • Validate that the SQL assertion correctly identifies the bug scenario

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Test Assertion Logic
The test uses an OR condition to check for correct SQL in both MySQL and PostgreSQL formats. However, this approach may pass even if one database generates incorrect SQL while the other is correct. Consider splitting into database-specific test cases or ensuring both formats are validated when applicable.

const hasCorrectJoin =
    sql.includes(
        "LEFT JOIN `game` `game` ON `game`.`id`=`match`.`game_id`  LEFT JOIN `participant` `participant` ON `participant`.`user_id`=`match`.`user_id` AND `participant`.`game_id`=`match`.`game_id`",
    ) ||
    sql.includes(
        `LEFT JOIN "game" "game" ON "game"."id"="match"."game_id"  LEFT JOIN "participant" "participant" ON "participant"."user_id"="match"."user_id" AND "participant"."game_id"="match"."game_id"`,
    )
expect(hasCorrectJoin).to.be.true

Order Difference
The order of JoinColumn array elements differs from the original issue. Original has game_id first, then user_id, but test has user_id first, then game_id. While this may not affect the bug reproduction, it's worth verifying this doesn't impact the test's ability to catch the specific caching issue mentioned.

@JoinColumn([
    { name: "user_id", referencedColumnName: "userId" },
    { name: "game_id", referencedColumnName: "gameId" },
])

The MySQL error unique constraint not found for FK_ for the entity participant appears if the order is changed. The order specified in JoinColumn[] and Participant.ts should be the same.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix incorrect ManyToOne relation callback

Correct the @ManyToOne relation on the game property by removing the invalid
(game) => game.id callback, as it incorrectly specifies the inverse side of the
relation.

test/functional/metadata-builder/relation-join-column-builder/entity/Match.ts [16-18]

 @JoinColumn({ name: "game_id", referencedColumnName: "id" })
-@ManyToOne(() => Game, (game) => game.id, { eager: true })
+@ManyToOne(() => Game, { eager: true })
 game: Game
  • Apply / Chat
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies that the (game) => game.id callback in the @ManyToOne decorator is semantically incorrect for defining an inverse relation, improving code correctness and adherence to TypeORM conventions.

Low
  • More

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

Added a test that shows that issue #7242 is resolved.

Unfortunately this test is failing with the latest codebase. 

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

@Cprakhar Could you verify the tests please?

@Cprakhar
Cprakhar force-pushed the test/test-for-issue-7242 branch from cf6aea9 to c7b6d50 Compare January 14, 2026 08:52
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo (Alpha)

🐞 Bugs (0) 📘 Rule Violations (0) 📎 Requirement Gaps (0) 💡 Suggestions (0)

Grey Divider

Great, No Issues Found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@Cprakhar Cprakhar changed the title test: add test for issue #7242 test: add tests to verify that correct column names are generated when using referencedColumnName in composite relations Jan 14, 2026
@Cprakhar Cprakhar changed the title test: add tests to verify that correct column names are generated when using referencedColumnName in composite relations test: add tests to verify that correct column names are generated when using referencedColumnName in composite relations Jan 14, 2026
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 80.881%. remained the same
when pulling c7b6d50 on Cprakhar:test/test-for-issue-7242
into 6f3788b on typeorm:master.

@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 for this amazing help @Cprakhar you rock 🚀

@gioboa
gioboa requested a review from G0maa January 14, 2026 11:14
@gioboa gioboa changed the title test: add tests to verify that correct column names are generated when using referencedColumnName in composite relations test: verify column names when using referencedColumnName in composite relations Jan 14, 2026
@gioboa
gioboa merged commit 13ab7c3 into typeorm:master Jan 14, 2026
40 of 41 checks passed
mgohin pushed a commit to mgohin/typeorm that referenced this pull request Jan 15, 2026
@Cprakhar
Cprakhar deleted the test/test-for-issue-7242 branch January 24, 2026 20:08
kranners pushed a commit to kranners/typeorm that referenced this pull request Mar 1, 2026
@pkuczynski pkuczynski added this to the 1.0 milestone Mar 8, 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.

TypeORM generates invalid column name for relation when referencedColumnName is used with another composite relation

5 participants