Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve issue on index naming strategy #10868

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/naming-strategy/DefaultNamingStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,9 @@ export class DefaultNamingStrategy implements NamingStrategyInterface {
columnNames: string[],
where?: string,
): string {
// sort incoming column names to avoid issue when ["id", "name"] and ["name", "id"] arrays
const clonedColumnNames = [...columnNames]
clonedColumnNames.sort()
const tableName = this.getTableName(tableOrName)
const replacedTableName = tableName.replace(".", "_")
let key = `${replacedTableName}_${clonedColumnNames.join("_")}`
let key = `${replacedTableName}_${columnNames.join("_")}`
if (where) key += `_${where}`

return "IDX_" + RandomGenerator.sha1(key).substr(0, 26)
Expand Down
17 changes: 17 additions & 0 deletions test/github-issues/10856/entity/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Column, Entity, Index, PrimaryGeneratedColumn } from "../../../../src"

@Entity({
name: "user",
})
@Index(["firstName", "lastName"])
@Index(["lastName", "firstName"])
export class User {
@PrimaryGeneratedColumn()
id: number

@Column({ nullable: true })
firstName: string

@Column()
lastName: string
}
36 changes: 36 additions & 0 deletions test/github-issues/10856/issue-10856.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import "reflect-metadata"
import {
createTestingConnections,
closeTestingConnections,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/index.js"
import { expect } from "chai"

describe("github issues > #10856 TypeORM Migration Bug: Duplicate Index Names in Generated Migration", () => {
let dataSources: DataSource[]

before(
async () =>
(dataSources = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchema: true,
enabledDrivers: ["mysql"],
logging: false,
})),
)

after(() => closeTestingConnections(dataSources))

it("Indexes on same column but different order generate a indexes with different names", () =>
Promise.all(
dataSources.map(async (dataSource) => {
const queryRunner = dataSource.createQueryRunner()
const schema = await queryRunner.getTable("user")
await queryRunner.release()
expect(schema!.indices[0].name).should.be.not.equal(
schema!.indices[1].name,
)
}),
))
})