Skip to content

test: modify tests to confirm ON CONFLICT ON CONSTRAINT on PostgreSQL/CockroachDB - #11908

Merged
G0maa merged 3 commits into
typeorm:masterfrom
Cprakhar:test/on-conflict-on-constraint
Jan 10, 2026
Merged

G0maa merged 3 commits into
typeorm:masterfrom
Cprakhar:test/on-conflict-on-constraint

Conversation

@Cprakhar

Copy link
Copy Markdown
Collaborator

Summary

This PR confirms that the ON CONFLICT ON CONSTRAINT clause is now supported by the orUpdate() method of the InsertQueryBuilder.

Changes

  • Included CockroachDB in the test
  • Updated the User entity

Related issues

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

8731 - PR Code Verified

Compliant requirements:

  • Support ON CONFLICT ON CONSTRAINT clause in PostgreSQL-like databases
  • Allow orUpdate method to handle constraint names as the second parameter
  • Work with PostgreSQL database driver

Requires further human verification:

  • Enable upsert operations when a key constraint violation happens - the test shows usage with a single column constraint (unique_first_name), but the ticket specifically mentions multi-column constraints like unique_pair (left_id, right_id). The test should verify multi-column constraint scenarios to fully validate the feature.
  • Support the unique_pair constraint scenario described in the ticket - the test uses a different constraint name and structure than what was described in the original issue.
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Incomplete Test Coverage

The test uses a single-column unique constraint (unique_first_name on first_name only), but the original ticket specifically requested support for multi-column constraints like unique_pair (left_id, right_id). The test should include a multi-column constraint scenario to properly validate the feature described in issue #8731.

@Unique("unique_first_name", ["first_name"])
export class User {
Test Logic Issue

The test at line 126 uses orUpdate with a column name array, but then at line 149 uses a constraint name string. However, both insertions use the same user2 values, so the second test (constraint name) may not properly validate the upsert behavior since the row already exists from the first test. The tests should use different data or ensure proper cleanup between scenarios.

    .orUpdate(["is_updated"], ["first_name"])
    .values(user2)
    .execute()
const loadedUser_3 = await UserRepository.find()
expect(loadedUser_3).not.to.be.eql([])
loadedUser_3.length.should.be.equal(1)
expect(loadedUser_3[0]).to.deep.include({
    first_name: "John",
    last_name: "Lenon",
    is_updated: "yes",
})

await UserRepository.remove(loadedUser_3)
const loadedUser_4 = await UserRepository.find()
expect(loadedUser_4).to.be.eql([])
// update while insertion duplicated row via unique's constraint name
await UserRepository.createQueryBuilder()
    .insert()
    .into(User)
    .values(user1)
    .execute()
await UserRepository.createQueryBuilder()
    .insert()
    .orUpdate(["is_updated"], "unique_first_name")

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

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

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
High-level
Relocate test to a dedicated file

The test for feature

Examples:

test/github-issues/1780/issue-1780.test.ts [12-168]
describe("github issues > #1780 Support for insertion ignore on duplicate error", () => {
    let connections: DataSource[]
    before(
        async () =>
            (connections = await createTestingConnections({
                entities: [User],
                schemaCreate: true,
                dropSchema: true,
                enabledDrivers: ["mysql", "mariadb", "postgres", "cockroachdb"],
            })),

 ... (clipped 147 lines)

Solution Walkthrough:

Before:

// File: 'test/github-issues/1780/issue-1780.test.ts'

describe("github issues > #1780 ...", () => {
  // ... setup for issue #1780

  it("should save one row without duplicate error in PostgreSQL/CockroachDB", () => {
    // ... test logic for issue #1780

    // Test for feature #8731 is added here
    // update while insertion duplicated row via unique's constraint name
    await UserRepository.createQueryBuilder()
        .insert()
        .orUpdate(["is_updated"], "unique_first_name")
        .values(user)
        .execute()
    // ... assertions
  });
});

After:

// File: 'test/github-issues/1780/issue-1780.test.ts'
// This file is reverted to its original state, or the parts
// testing feature #8731 are removed.

// New File: 'test/github-issues/8731/issue-8731.test.ts'
describe("github issues > #8731 ON CONFLICT ON CONSTRAINT", () => {
  // ... setup specific for issue #8731

  it("should update on conflict with a constraint name", () => {
    await UserRepository.createQueryBuilder()
        .insert()
        .orUpdate(["is_updated"], "unique_first_name")
        .values(user)
        .execute()
    // ... assertions
  });
});
Suggestion importance[1-10]: 6

__

Why: The suggestion correctly identifies a test organization issue that harms maintainability, as the test for feature

Low
  • More

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

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@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 doing this kind of activity @Cprakhar
Really appreciated 🙏

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

🚀

@gioboa
gioboa requested review from G0maa and naorpeled January 10, 2026 12:10
@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@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

@G0maa
G0maa merged commit db2369c into typeorm:master Jan 10, 2026
36 checks passed
mgohin pushed a commit to mgohin/typeorm that referenced this pull request Jan 15, 2026
@Cprakhar
Cprakhar deleted the test/on-conflict-on-constraint branch January 24, 2026 20:06
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.

4 participants