Skip to content

test: verify synchronize true works with multiple relations - #11889

Merged
gioboa merged 10 commits into
typeorm:masterfrom
Cprakhar:test/add-tests-for-issue-7738
Jan 18, 2026
Merged

gioboa merged 10 commits into
typeorm:masterfrom
Cprakhar:test/add-tests-for-issue-7738

Conversation

@Cprakhar

@Cprakhar Cprakhar commented Jan 4, 2026 •

Copy link
Copy Markdown
Collaborator

Description of change

Added tests for issue #7738. I attempted to replicate the issue, but everything functioned properly.
Also add one test as specified at #7738 (comment).

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as Fixes #00000
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

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

7738 - PR Code Verified

Compliant requirements:

  • Test uses PostgreSQL driver (enabledDrivers: ["postgres"])
  • Test includes Professor and Subject entities with multiple relations to same table
  • Test verifies that second synchronization does not fail (await expect(dataSource.synchronize()).to.not.be.rejected)
  • Test creates and saves entities to verify foreign key relationships work correctly

Requires further human verification:

  • Whether the test actually reproduces the original bug condition (the PR description states "I attempted to replicate the issue, but everything functioned properly")
  • Whether the fix for issue Constraint already exists error on second run with synchronize #7738 was already implemented elsewhere, making this test verify existing functionality rather than catching a regression
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Test Coverage Gap

The test includes Site and SiteLocation entities which test a OneToOne relationship, but these entities are not mentioned in the original issue #7738. The issue specifically describes a problem with multiple ManyToOne relations to the same table. While additional test coverage is good, it's unclear if these entities are necessary for reproducing the specific bug or if they add unrelated test scenarios.

entities: [Professor, Subject, Site, SiteLocation],
dropSchema: true,
Inconsistent Configuration

The Professor entity has synchronize: true explicitly set in the @Entity decorator, but the Site and SiteLocation entities do not. This inconsistency might affect test behavior. Either all entities should have explicit synchronize settings or none should, to ensure consistent test conditions.

@Entity({ synchronize: true })
export class Professor {

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

qodo-free-for-open-source-projects Bot commented Jan 4, 2026 •

Copy link
Copy Markdown

PR Code Suggestions ✨

Latest suggestions up to 24d8ab7

CategorySuggestion                                                                                                                                    Impact
General
Use correct Chai promise assertion

Replace the to.not.be.rejected assertion with the more idiomatic to.be.fulfilled
from chai-as-promised to assert that the promise resolves successfully.

test/functional/connection/connection.test.ts [435-446]

 it("should not fail with constraint already exists error when synchronizing multiple times", () =>
     Promise.all(
         connections.map(async (dataSource) => {
             // First synchronization
             await dataSource.synchronize()
 
             // Second synchronization should not fail with constraint error
-            await expect(dataSource.synchronize()).to.not.be.rejected
+            await expect(dataSource.synchronize()).to.be.fulfilled
             ...
         }),
     ))

[To ensure code accuracy, apply this suggestion manually]

Suggestion importance[1-10]: 3

__

Why: The suggestion correctly points out that to.be.fulfilled is a more idiomatic assertion for promise success, but the existing code with to.not.be.rejected is also valid and functional, making this a low-impact style improvement.

Low
  • More

Previous suggestions

Suggestions up to commit 42321be
CategorySuggestion                                                                                                                                    Impact
General
Add relation null checks before access

Add explicit expect checks to verify that the professor and assistant relations
on loadedSubject are not null or undefined before accessing their properties.

test/functional/connection/connection.test.ts [492-494]

 const loadedSubject = await subjectRepo.findOne({
     where: { id: subject.id },
     relations: ["professor", "assistant"],
 })
 
 ...
 
 expect(loadedSubject).to.not.be.null
+expect(loadedSubject!.professor).to.not.be.undefined
+expect(loadedSubject!.assistant).to.not.be.undefined
 expect(loadedSubject!.professor.name).to.equal("Dr. Smith")
 expect(loadedSubject!.assistant.name).to.equal("Dr. Jones")
Suggestion importance[1-10]: 5

__

Why: Adding explicit checks for the existence of the professor and assistant relations makes the test more robust and provides clearer failure messages if the relations are not loaded correctly.

Low
Possible issue
Fix invalid Chai assertion syntax

Replace the to.not.be.rejected Chai assertion with to.be.fulfilled for better
readability when checking for a successful promise resolution.

test/functional/connection/connection.test.ts [441-442]

 // Second synchronization should not fail with constraint error
-await expect(dataSource.synchronize()).to.not.be.rejected
+await expect(dataSource.synchronize()).to.be.fulfilled
Suggestion importance[1-10]: 3

__

Why: The assertion to.not.be.rejected is valid with chai-as-promised and works correctly, but using to.be.fulfilled is more direct and slightly improves readability.

Low
Suggestions up to commit bf081f6
CategorySuggestion                                                                                                                                    Impact
General
Use more explicit promise assertion

Replace the promise assertion to.not.be.rejected with the more explicit
to.be.fulfilled to better express the test's intent.

test/functional/connection/connection.test.ts [435-442]

 it("should not fail with constraint already exists error when synchronizing multiple times", () =>
     Promise.all(
         connections.map(async (dataSource) => {
             // First synchronization
             await dataSource.synchronize()
 
             // Second synchronization should not fail with constraint error
-            await expect(dataSource.synchronize()).to.not.be.rejected
+            await expect(dataSource.synchronize()).to.be.fulfilled
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly identifies that to.be.fulfilled is a more explicit and idiomatic assertion than to.not.be.rejected for this test case, improving code clarity.

Low
Suggestions up to commit d658bf5
CategorySuggestion                                                                                                                                    Impact
General
Remove unrelated test logic

Remove the unrelated test logic for Site and SiteLocation to make the test more
focused on its primary goal of verifying synchronization with multiple foreign
keys.

test/functional/connection/connection.test.ts [468-494]

 const loadedSubject = await subjectRepo.findOne({
     where: { id: subject.id },
     relations: ["professor", "assistant"],
 })
 
-const site = siteRepo.create({ name: "Main Campus" })
-await siteRepo.save(site)
-
-const siteLocation = siteLocationRepo.create({
-    address: "123 Main St",
-    site: site,
-})
-await siteLocationRepo.save(siteLocation)
-
-const loadedSiteLocation = await siteLocationRepo.findOne({
-    where: { id: siteLocation.id },
-    relations: ["site"],
-})
-
-expect(loadedSiteLocation).to.not.be.null
-expect(loadedSiteLocation!.site.name).to.equal(
-    "Main Campus",
-)
-
 expect(loadedSubject).to.not.be.null
 expect(loadedSubject!.professor.name).to.equal("Dr. Smith")
 expect(loadedSubject!.assistant.name).to.equal("Dr. Jones")
Suggestion importance[1-10]: 5

__

Why: The suggestion correctly identifies that the test code for Site and SiteLocation is not relevant to the test's primary goal, and removing it would improve the test's focus and maintainability.

Low
Use modern object-based relations syntax

Update the relations option in subjectRepo.findOne to use the modern
object-based syntax instead of the deprecated string array.

test/functional/connection/connection.test.ts [468-471]

 const loadedSubject = await subjectRepo.findOne({
     where: { id: subject.id },
-    relations: ["professor", "assistant"],
+    relations: {
+        professor: true,
+        assistant: true,
+    },
 })
Suggestion importance[1-10]: 4

__

Why: The suggestion correctly proposes updating the deprecated string-based relations syntax to the modern object-based syntax, which improves code style and aligns with current best practices.

Low

@Cprakhar
Cprakhar force-pushed the test/add-tests-for-issue-7738 branch from d658bf5 to bf081f6 Compare January 4, 2026 10:57
@coveralls

coveralls commented Jan 4, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.935% (-0.002%) from 80.937%
when pulling 24d8ab7 on Cprakhar:test/add-tests-for-issue-7738
into 77b6ca9 on typeorm:master.

@gioboa
gioboa requested review from G0maa, alumni and naorpeled January 7, 2026 21:59

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

I attempted to replicate the issue, but everything functioned properly.

Can I ask you to confirm that? Is the latest code working for you @petkovicdanilo @matthew-emw ?

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

qodo-free-for-open-source-projects Bot commented Jan 13, 2026 •

Copy link
Copy Markdown

Code Review by Qodo (Alpha)

(Review updated until commit 24d8ab7)

🐞 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


Previous review results

Results up to commit 42321be


🐞 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

@gioboa gioboa changed the title test: add tests to verify that synchronize: true is working perfectly with multiple relations test: verify synchronize true works with multiple relations Jan 13, 2026
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 24d8ab7

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

Great 👏 thanks @Cprakhar

@gioboa
gioboa merged commit a0d190d into typeorm:master Jan 18, 2026
36 checks passed
@Cprakhar
Cprakhar deleted the test/add-tests-for-issue-7738 branch January 24, 2026 20:07
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.

5 participants