Table indexes needlessly recreated when generating migrations
See original GitHub issueIssue type:
[ ] question [x ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ x] mysql
/ mariadb
(using mysql 8)
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[x ] 0.2.19
Steps to reproduce or a small repository showing the problem:
- Create models below
- Generate and run a migration (generated code shown below).
- Make no changes to the models
- Generate another migration (generated code shown below)
Results: Migration drops and recreates many indexes
Expected: No changes in the migration since the model has not changed
Note that the only change in the migration seems to be from the index name REL_36c1bea209538ff07049dd748a
in the initial creation of the table to IDX_36c1bea209538ff07049dd748a
in the migration.
Models
@Entity({ name: 'users2' })
export class Users {
@PrimaryGeneratedColumn()
id!: string;
}
@Entity({ name: 'simple_example' })
@Unique(['userId', 'choice'])
export class SimpleExample {
@PrimaryGeneratedColumn()
id!: string;
@Column({ name: 'user_id', nullable: false })
userId!: UserID;
@Column({ name: 'choice', nullable: false })
choice!: number;
@OneToOne(() => Users)
@JoinColumn({ name: ReferralSurvey.columnNames.userId })
user!: UserModel;
}
Initial migration
import {MigrationInterface, QueryRunner} from "typeorm";
export class createExample1574282003669 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("CREATE TABLE `users2` (`id` int NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined);
await queryRunner.query("CREATE TABLE `simple_example` (`id` int NOT NULL AUTO_INCREMENT, `user_id` int NOT NULL, `choice` int NOT NULL, UNIQUE INDEX `IDX_a1ebc46dd9a46cf638f43239fe` (`user_id`, `choice`), UNIQUE INDEX `REL_36c1bea209538ff07049dd748a` (`user_id`), PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined);
await queryRunner.query("ALTER TABLE `simple_example` ADD CONSTRAINT `FK_36c1bea209538ff07049dd748ad` FOREIGN KEY (`user_id`) REFERENCES `users2`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION", undefined);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("ALTER TABLE `simple_example` DROP FOREIGN KEY `FK_36c1bea209538ff07049dd748ad`", undefined);
await queryRunner.query("DROP INDEX `REL_36c1bea209538ff07049dd748a` ON `simple_example`", undefined);
await queryRunner.query("DROP INDEX `IDX_a1ebc46dd9a46cf638f43239fe` ON `simple_example`", undefined);
await queryRunner.query("DROP TABLE `simple_example`", undefined);
await queryRunner.query("DROP TABLE `users2`", undefined);
}
}
Second migration with no changes to the model
import {MigrationInterface, QueryRunner} from "typeorm";
export class generateAfterNoChanges1574282199014 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("ALTER TABLE `simple_example` DROP FOREIGN KEY `FK_36c1bea209538ff07049dd748ad`", undefined);
await queryRunner.query("DROP INDEX `IDX_a1ebc46dd9a46cf638f43239fe` ON `simple_example`", undefined);
await queryRunner.query("ALTER TABLE `simple_example` ADD UNIQUE INDEX `IDX_36c1bea209538ff07049dd748a` (`user_id`)", undefined);
await queryRunner.query("CREATE UNIQUE INDEX `IDX_a1ebc46dd9a46cf638f43239fe` ON `simple_example` (`user_id`, `choice`)", undefined);
await queryRunner.query("ALTER TABLE `simple_example` ADD CONSTRAINT `FK_36c1bea209538ff07049dd748ad` FOREIGN KEY (`user_id`) REFERENCES `users2`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION", undefined);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query("ALTER TABLE `simple_example` DROP FOREIGN KEY `FK_36c1bea209538ff07049dd748ad`", undefined);
await queryRunner.query("DROP INDEX `IDX_a1ebc46dd9a46cf638f43239fe` ON `simple_example`", undefined);
await queryRunner.query("ALTER TABLE `simple_example` DROP INDEX `IDX_36c1bea209538ff07049dd748a`", undefined);
await queryRunner.query("CREATE UNIQUE INDEX `IDX_a1ebc46dd9a46cf638f43239fe` ON `simple_example` (`user_id`, `choice`)", undefined);
await queryRunner.query("ALTER TABLE `simple_example` ADD CONSTRAINT `FK_36c1bea209538ff07049dd748ad` FOREIGN KEY (`user_id`) REFERENCES `users2`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION", undefined);
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:9
In column config. Like
I am still able to reproduce this issue in 0.3.4