fix: solve migration issue with primary key renaming#11992
fix: solve migration issue with primary key renaming#11992gioboa wants to merge 10 commits intotypeorm:masterfrom
Conversation
User descriptionClose #11636 Description of changePull-Request Checklist
PR TypeBug fix, Tests Description
Diagram Walkthroughflowchart LR
A["Schema Builder"] -->|detects PK constraint change| B["findChangedColumns"]
B -->|compares primaryKeyConstraintName| C["Driver Methods"]
C -->|generates migration| D["updatePrimaryKeys"]
D -->|uses correct column source| E["Database"]
F["Test Suite"] -->|validates| A
|
| Relevant files | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Documentation | |||||||||||
| Bug fix | 5 files
| ||||||||||
| Tests | 1 files
|
commit: |
PR Code Suggestions ✨Latest suggestions up to 65b3e9d
Previous suggestionsSuggestions up to commit a91d51d
✅ Suggestions up to commit b5cadc3
✅ Suggestions up to commit b08e73b
✅ Suggestions up to commit 303a0b0
✅ Suggestions up to commit a643414
✅ Suggestions up to commit f22f8a8
✅ Suggestions up to commit 7f2cfe8
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Code Review by Qodo
✅ 1.
|
test/functional/schema-builder/change-primary-key-constraint.test.ts
Outdated
Show resolved
Hide resolved
|
Persistent review updated to latest commit f22f8a8 |
|
Persistent review updated to latest commit a643414 |
|
Persistent review updated to latest commit 42954c7 |
|
Persistent review updated to latest commit 9901bb4 |
|
Persistent review updated to latest commit b08e73b |
|
Persistent review updated to latest commit 303a0b0 |
test/functional/schema-builder/change-primary-key-constraint.test.ts
Outdated
Show resolved
Hide resolved
|
Persistent review updated to latest commit d47979d |
|
Persistent review updated to latest commit b5cadc3 |
|
Persistent review updated to latest commit a91d51d |
|
Persistent review updated to latest commit 65b3e9d |
| const effectiveOldPkName = primaryTableColumns[0] | ||
| ?.primaryKeyConstraintName | ||
| ? primaryTableColumns[0].primaryKeyConstraintName | ||
| : this.connection.namingStrategy.primaryKeyName( | ||
| table, | ||
| primaryTableColumns.map((column) => column.name), | ||
| ) | ||
| const effectiveNewPkName = primaryMetadataColumns[0] | ||
| ?.primaryKeyConstraintName | ||
| ? primaryMetadataColumns[0].primaryKeyConstraintName | ||
| : this.connection.namingStrategy.primaryKeyName( | ||
| table, | ||
| primaryMetadataColumns.map( | ||
| (column) => column.databaseName, | ||
| ), | ||
| ) | ||
|
|
||
| const pkNameChanged = | ||
| primaryTableColumns.length > 0 && | ||
| primaryMetadataColumns.length > 0 && | ||
| primaryTableColumns.length === primaryMetadataColumns.length && | ||
| effectiveOldPkName !== effectiveNewPkName && | ||
| (DriverUtils.isPostgresFamily(this.connection.driver) || | ||
| this.connection.driver.options.type === "mssql" || | ||
| this.connection.driver.options.type === "oracle" || | ||
| this.connection.driver.options.type === "cockroachdb") | ||
|
|
||
| if ( | ||
| primaryTableColumns.length !== primaryMetadataColumns.length && | ||
| primaryMetadataColumns.length > 1 | ||
| (primaryTableColumns.length !== primaryMetadataColumns.length && | ||
| primaryMetadataColumns.length > 1) || | ||
| (primaryMetadataColumns.length === primaryTableColumns.length && | ||
| primaryMetadataColumns.length > 0 && | ||
| pkNameChanged) |
There was a problem hiding this comment.
1. Spurious rename migrations for existing dbs 🐞 Bug ✓ Correctness
The new pkNameChanged check in RdbmsSchemaBuilder.ts triggers a PK rename migration whenever the DB's actual PK constraint name differs from what the entity specifies. When an existing DB has a PK constraint whose name doesn't match the current naming strategy (e.g., created via a custom migration), but the entity has no explicit primaryKeyConstraintName, pkNameChanged = true and an unexpected rename migration is generated on the next schema sync.
Agent Prompt
## Issue description
The `pkNameChanged` condition in `RdbmsSchemaBuilder.updatePrimaryKeys()` fires whenever the DB's actual PK constraint name differs from what the entity specifies — even when the entity has NO explicit `primaryKeyConstraintName`. This causes unexpected rename migrations for any existing DB whose PK constraint name doesn't match the current naming strategy.
## Issue Context
In `loadTables`, `primaryKeyConstraintName` is only set when the DB name ≠ naming strategy name. So `effectiveOldPkName` can be a DB-side custom name while `effectiveNewPkName` is the naming strategy result, causing a false positive `pkNameChanged = true`.
The PR itself worked around this by adding explicit `primaryKeyConstraintName` to `FooWithSpecialChars.ts` to prevent the existing test from failing.
## Fix Focus Areas
- src/schema-builder/RdbmsSchemaBuilder.ts[856-888]
- test/github-issues/10955/entity/FooWithSpecialChars.ts[11-13]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
Close #11636
Description of change
Pull-Request Checklist
masterbranchFixes #00000tests/**.test.ts)docs/docs/**.md)