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

TypeORM Migration Bug: Duplicate Index Names in Generated Migration #10856

Open
1 of 18 tasks
kkeolmusae opened this issue Apr 29, 2024 · 0 comments · May be fixed by #10868
Open
1 of 18 tasks

TypeORM Migration Bug: Duplicate Index Names in Generated Migration #10856

kkeolmusae opened this issue Apr 29, 2024 · 0 comments · May be fixed by #10868

Comments

@kkeolmusae
Copy link

Issue description

As you can see, the generated migration script has identical index names, even though the order of columns in the entity definition is different for the two indices. This is causing a conflict and potentially invalid database operations due to duplicate index names.

Expected Behavior

The migration script should generate unique index names based on the column order specified in the entity definition, ensuring proper creation and deletion of indices in the database

Actual Behavior

When running yarn migration:generate in a TypeORM project, the generated migration script creates duplicate index names despite the entity file specifying two distinct indices with different column orders.

Entity Definition

Here is the relevant part of the entity definition that specifies two different indices:

import { Column, Entity, Index, PrimaryColumn } from "typeorm";

@Entity()
@Index(["aCrc", "bCrc", "c"])
@Index(["bCrc", "aCrc", "c"])
export class TableA {
  @PrimaryColumn({ type: "binary", length: 20 })
  a: Buffer;

  @PrimaryColumn({ type: "binary", length: 20 })
  b: Buffer;

  @Column({ type: "int", unsigned: true })
  bCrc: number;

  @Column({ type: "int", unsigned: true })
  aCrc: number;

  @Column({ type: "double", unsigned: true })
  c: number;
}

Generated Migration Script

However, the generated migration script shows two identical index names, despite the indices being defined with different orders:

import { MigrationInterface, QueryRunner } from "typeorm";

export class CreateTables1714372786425 implements MigrationInterface {
  name = "CreateTables1714372786425";

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE \`table_a\` (
        \`a\` binary(20) NOT NULL, 
        \`b\` binary(20) NOT NULL, 
        \`bCrc\` int UNSIGNED NOT NULL, 
        \`aCrc\` int UNSIGNED NOT NULL, 
        \`c\` double UNSIGNED NOT NULL, 
        INDEX \`IDX_636863777022eb2cd64c5407f2\` (\`bCrc\`, \`aCrc\`, \`c\`), 
        INDEX \`IDX_636863777022eb2cd64c5407f2\` (\`aCrc\`, \`bCrc\`, \`c\`), 
        PRIMARY KEY (\`a\`, \`b\`)) 
      ENGINE=InnoDB`
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP INDEX \`IDX_636863777022eb2cd64c5407f2\` ON \`table_a\``);
    await queryRunner.query(`DROP INDEX \`IDX_636863777022eb2cd64c5407f2\` ON \`table_a\``);
    await queryRunner.query(`DROP TABLE \`table_a\``);
  }
}

Steps to reproduce

Steps to Reproduce

To reproduce the issue of duplicate index names in TypeORM migrations, follow these steps:

  1. Create a New TypeORM Project

    • Initialize a new TypeORM project with the necessary setup for migrations.
    • Ensure you have a valid database connection configured in ormconfig.json or a similar configuration file.
  2. Create an Entity with Two Different Indices

    • Create an entity that has at least two index definitions with different column orders. Here is an example entity:

      import { Column, Entity, Index, PrimaryColumn } from "typeorm";
      
      @Entity()
      @Index(["aCrc", "bCrc", "c"]) // First index
      @Index(["bCrc", "aCrc", "c"]) // Second index
      export class TableA {
        @PrimaryColumn({ type: "binary", length: 20 })
        a: Buffer;
      
        @PrimaryColumn({ type: "binary", length: 20 })
        b: Buffer;
      
        @Column({ type: "int", unsigned: true })
        bCrc: number;
      
        @Column({ type: "int", unsigned: true })
        aCrc: number;
      
        @Column({ type: "double", unsigned: true })
        c: number;
      }
  3. Generate a Migration Script

    • Run the migration generation command, typically yarn migration:generate --name=CreateTableA.
    • This will generate a migration script that creates the table with the defined indices.
  4. Inspect the Generated Migration Script

    • Open the generated migration script (e.g., src/migration/1633020286482-CreateTableA.ts).
    • Check the index definitions in the migration script to see if they have the same name despite being defined with different column orders in the entity.

If the above steps are followed, you should see that the generated migration script has duplicate index names, indicating the issue with the migration generation process in TypeORM. This behavior might lead to database conflicts or unexpected results when running the migrations.

My Environment

Dependency Version
Operating System MAC
Node.js version 20.12.0
Typescript version 5.1.3
TypeORM version 0.3.17
nestjs/typeorm 10.0.0

Additional Context

No response

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, but I don't know how to start. I would need guidance.

alenap93 added a commit to alenap93/typeorm that referenced this issue May 1, 2024
@alenap93 alenap93 linked a pull request May 1, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant