Skip to content

Migration generate continuously repeats entries #3280

@ktwbc

Description

@ktwbc

Issue type:

[ ] question
[ X] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[X ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[X ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

When building model files and relying on building and running migrations, TypeORM will constantly drop and add the same indexes over and over.

Table A:

import { TableB } from './table-b';

@Entity('table_a')
export class TableA {

  @PrimaryColumn('uuid')
  id: string;

  @Column({ nullable: true })
  etag: string;

  @ManyToOne(type => TableB, item => item.id)
  @JoinColumn({name: 'category_id'})
  category: TableB;

  @Column({ nullable: true })
  file_name: string;


}

Table B:


@Entity('table_b')
export class TableB {

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: true})
  @Index("IDX_2a8279b83232e2243609048a69")
  uuid: string;

  @Column()
  title: string;


}

Steps:
a. build model files per above
b. Run migrate build:
ts-node ./node_modules/typeorm/cli.js migration:generate -f ormconfig-local -c migrate -n 'mySql'
c. view build migration:


export class mySql1545169032433 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("CREATE TABLE `table_b` (`id` int NOT NULL AUTO_INCREMENT, `uuid` varchar(255) NULL, `title` varchar(255) NOT NULL, INDEX `IDX_2a8279b83232e2243609048a69` (`uuid`), PRIMARY KEY (`id`)) ENGINE=InnoDB");
        await queryRunner.query("CREATE TABLE `table_a` (`id` varchar(255) NOT NULL, `etag` varchar(255) NULL, `file_name` varchar(255) NULL, `category_id` int NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB");
        await queryRunner.query("ALTER TABLE `table_a` ADD CONSTRAINT `FK_27932cc33301f96d232624ed5ea` FOREIGN KEY (`category_id`) REFERENCES `table_b`(`id`)");
    }

    public async down(queryRunner: QueryRunner): Promise<any> {

    }

}

d. Run migration:
ts-node ./node_modules/typeorm/cli.js migration:run -f ormconfig-local -c migrate

e. Run a migration:generate again

Expect:
No migration file should be generated because nothing has changed

Instead:
A new migration file is generated dropping and adding indexes. This actually happens on indexes and foreign keys. It makes no difference whether you try to name the IDX/FK yourself or let TypeOrm do it. On my 30 table database, I get a huge list of every index and foreign keys being removed and added, it never recognizes nothing has changed.


export class mySql1545169077016 implements MigrationInterface {

    public async up(queryRunner: QueryRunner): Promise<any> {

        await queryRunner.query("DROP INDEX `IDX_2a8279b83232e2243609048a69` ON `table_b`");
        await queryRunner.query("CREATE INDEX `IDX_2a8279b83232e2243609048a69` ON `table_b`(`uuid`)");

    }

    public async down(queryRunner: QueryRunner): Promise<any> {
        await queryRunner.query("DROP INDEX `IDX_2a8279b83232e2243609048a69` ON `table_b`");
        await queryRunner.query("CREATE INDEX `IDX_2a8279b83232e2243609048a69` ON `table_b`(`site_id`, `uuid`)");
    }

}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions