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

Migration generate continuously repeats entries #3280

Closed
ktwbc opened this issue Dec 18, 2018 · 13 comments
Closed

Migration generate continuously repeats entries #3280

ktwbc opened this issue Dec 18, 2018 · 13 comments

Comments

@ktwbc
Copy link

ktwbc commented Dec 18, 2018

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`)");
    }

}
@ktwbc
Copy link
Author

ktwbc commented Dec 18, 2018

I saw the comment on #3277 but even if you are not naming things, every new migration file will drop and readd things that are already there. Even using @Index() the type hint shows you can provide a name, i.e. @Index("my_index_name") but it will always be dropped and readded whenever you make a new migration file.

With a list of 30 tables, every time I run a new build migration, I get a huge list of changes I have to delete most of them to find my the one I want.

@pleerock
Copy link
Member

Would be helpful if you put all this code into minimal possible reproduction repository and let us check it.

@UnsolvedCypher
Copy link

I don't know if it was caused by the same issue as @ktwbc was having, but I also ran into the issue of TypeORM generating these migrations endlessly. My issue turned out to be that I named the class of the entity differently from the filename. Perhaps TypeORM could ensure that the exported class names match the filenames and throw an error otherwise?

@tpiecora
Copy link

tpiecora commented Feb 7, 2020

I seem to be having a similar issue where I have a view entity for a postgres view and whenever I try to generate a migration, the migration wants to drop the view twice, and the revert wants to put it back twice.

@programateus
Copy link

I'm having this same issue, but only with mysql/mariadb, tested on a postgres db and It works. You can recreate this just by creating an entity and generating a migration, then run the migration and generate again, it gonna be the same code from the first migration. Hope this can get some attention... :(

@annvakulchyk
Copy link

The same issue here, typeorm version 0.2.24, connection type MySQL, generate migration creates the same entities over and over again, it doesn't produce diff

@zaro
Copy link
Contributor

zaro commented Apr 13, 2020

Same here. Connection type is postgres (pg v12), was working fine with 0.2.22, upgrading to 0.2.24 causes generate to generate migrations every time it is run, w/o any changes to the entities themselves.
The bulk of generated migrations is dropping FK and recreating them.

This is a real blocker for moving to 0.2.24

@zaro
Copy link
Contributor

zaro commented Apr 13, 2020

@pleerock here is a PR with testcase #5869

@danielkv
Copy link

danielkv commented Oct 12, 2020

It seems to be fixed in 0.2.28

@imnotjames
Copy link
Contributor

🎉 Code was merged in so I'll be closing this out.

@Caperious
Copy link

This is issue is still present when using mariadb with typeorm version 0.2.31.

Steps to reproduce:

  • Empty database schema
  • Generate migration for entities
  • Run the migration
  • Generate the migrations again -> Queries altering the columns, droping and recreating the FK-s etc.

I don't think this is supposed to happen.

@jimytc
Copy link

jimytc commented Mar 27, 2021

Not sure if our case is related to this.
Looks like there will be two DDL queries generated for joining table when using ManyToMany annotation.

Here's a sample repository.
https://github.com/jimytc/typeorm-repeated-migrations
I put three commits for different ways to define entities and joining tables.

We could see migration files in the first two commits.
There are two DDL queries for joining table but the table names are different.
One is post_to_tag and the other is tag_to_post.
The differences are 1) order of FKs and 2) PK configuration.

Entity definition demonstrated from Typeorm doc.

jimytc/typeorm-repeated-migrations@0814ffc#diff-0e61419b393c2da56e1d2f8cb965d9c8d735dcd4b345be31def32c3a5e69d2e5

Define joining table with naming convention demonstrated in Typeorm doc.

jimytc/typeorm-repeated-migrations@955f6c0#diff-89cfe6f26ee888bc22c677a1900978475e7757912418630f8179ec2a238a5640

Self-defined joining table and specify name in JoinTable annotation.

jimytc/typeorm-repeated-migrations@87ff51d#diff-1de678b75047b2292793a36c1a20ecf68ac9b218d34142f8506eb1e96580bb9f

@jimytc
Copy link

jimytc commented Mar 27, 2021

Please ignore my above comment since I found another issue related to it.
#5688

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests