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

Composite keys always re added to migration with migration:generate #8254

Open
KirianCaumes opened this issue Oct 7, 2021 · 0 comments
Open

Comments

@KirianCaumes
Copy link

Issue Description

Composite keys always re added to migration with migration:generate

Expected Behavior

Fields should not be re added in each new migrations.

Actual Behavior

Migrations like this are generated, despite the fields are already in DB and generated in the previous migration.

class MyMigration123456789 implements MigrationInterface {
    name = 'MyMigration123456789'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TABLE "a_to_b" DROP CONSTRAINT "FK_a675a44c6c2ee41bcbb73024360"`);
        await queryRunner.query(`ALTER TABLE "a_to_b" DROP CONSTRAINT "FK_8325ba645245ab1a1b176ae1473"`);
        await queryRunner.query(`ALTER TABLE "a_to_b" ALTER COLUMN "a_id" int`);
        await queryRunner.query(`ALTER TABLE "a_to_b" ALTER COLUMN "b_id" int`);
        await queryRunner.query(`ALTER TABLE "a_to_b" ALTER COLUMN "a_id" int`);
        await queryRunner.query(`ALTER TABLE "a_to_b" ALTER COLUMN "b_id" int`);
        await queryRunner.query(`ALTER TABLE "a_to_b" ADD CONSTRAINT "FK_a675a44c6c2ee41bcbb73024360" FOREIGN KEY ("a_id") REFERENCES "step"("a_id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
        await queryRunner.query(`ALTER TABLE "a_to_b" ADD CONSTRAINT "FK_8325ba645245ab1a1b176ae1473" FOREIGN KEY ("b_id") REFERENCES "b"("b_id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
    }
}

Steps to Reproduce

1 - Create entities like this:

@Entity()
export default class A {
    @PrimaryGeneratedColumn()
    aId: number

    @Column({ length: 255 })
    name: string

    @OneToMany(() => AToB, aToB => aToB.a)
    aToBs: AToB[]
}

@Entity()
export default class B {
    @PrimaryGeneratedColumn()
    bId: number

    @Column({ length: 255 })
    name: string

    @OneToMany(() => AToB, aToB => aToB.b)
    aToBs: AToB[]
}

@Entity()
export default class AToB {
    @Column()
    isEnable: boolean

    @ApiProperty({ type: () => Step })
    @ManyToOne(() => A, a=> a.aToBs, { primary: true })
    @JoinColumn({ name: 'a_id' })
    a: A

    @Column({ nullable: true, primary: true })
    aId: number

    @ApiProperty({ type: () => ProjectType })
    @ManyToOne(() => B, b=> b.aToBs, { primary: true })
    @JoinColumn({ name: 'b_Id' })
    b: B

    @Column({ nullable: true, primary: true })
    bId: number
}

2 - Generate new migration.

My command:

ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/ormconfig.ts migration:generate -n MyMigration

My Environment

Dependency Version
Operating System ubuntu:18.04
Node.js version 14.17.6
Typescript version 4.3.5
TypeORM version 0.2.38

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres no
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver Yes (Only tested here)

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

  • ✖️ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✅ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

It might be related to #8157, but I'm not sure.

Thanks a lot guys for the help 🙏

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

1 participant