Skip to content

migration:generate recreates defaults for no reason each migration #9750

Closed
@Satont

Description

@Satont

Issue description

When i generates new migrations, then typeorm always trying to recreate some defualts

Expected Behavior

Typeorm shouldn't try to recreate defauls

Actual Behavior

Typeorm always recreates defaults.

Steps to reproduce

This is what typeorm generates

await queryRunner.query(`ALTER TABLE "channel_events_donations" ADD "donateId" character varying`);
        await queryRunner.query(`ALTER TABLE "channels_modules_settings" ALTER COLUMN "id" DROP DEFAULT`);
        await queryRunner.query(`ALTER TABLE "channels_modules_settings" ALTER COLUMN "id" SET DEFAULT gen_random_uuid()`);
        await queryRunner.query(`ALTER TABLE "channels_requested_songs" ALTER COLUMN "id" DROP DEFAULT`);
        await queryRunner.query(`ALTER TABLE "channels_requested_songs" ALTER COLUMN "id" SET DEFAULT gen_random_uuid()`);
        ```

My entity

@entity('channels_modules_settings')
export class ChannelModuleSettings {
@PrimaryColumn('uuid', {
primary: true,
name: 'id',
default: () => 'gen_random_uuid()',
})
id: string;

@column('enum', { enum: ModuleType })
type: ModuleType;

@column('jsonb')
settings: Record<string, any>;

@column()
channelId: string;

@manytoone('Channel', 'modules')
@joincolumn({ name: 'channelId' })
channel?: Relation;
}


### My Environment

| Dependency          | Version  |
| ---                 | ---      |
| Operating System    |          |
| Node.js version     | 18.2.1  |
| Typescript version  | 4.9 |
| TypeORM version     | 0.3.10  |


### Additional Context

_No response_

### Relevant Database Driver(s)

- [ ] aurora-mysql
- [ ] aurora-postgres
- [ ] better-sqlite3
- [ ] cockroachdb
- [ ] cordova
- [ ] expo
- [ ] mongodb
- [ ] mysql
- [ ] nativescript
- [ ] oracle
- [X] 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.

Activity

jleverenz

jleverenz commented on Feb 2, 2023

@jleverenz

I just ran into a similar issue, due to the default: () => 'gen_random_uuid()' configuration. Re-reading the TypeORM docs, switching to one of the Generated decorators solved it for me, it might work for you. Try:

@Entity('channels_modules_settings')
export class ChannelModuleSettings {
  @PrimaryGeneratedColumn('uuid')
  id: string;
  
  // ...
}

I think you might get the same effective sql generated, without the repeated migrations.

kacsablabla

kacsablabla commented on May 23, 2023

@kacsablabla

I've had the same issue when upgrading typeorm 0.3.11 to 0.3.16. I reverted it, and works again.

N-Andronopoulos

N-Andronopoulos commented on May 28, 2023

@N-Andronopoulos

I have the same issue with mysql2 v2 or v3 driver after version 0.3.10 (tried 0.3.12 and 0.3.16 issue exists in both).
It tries to recreate ids and relations FKs etc.

We use @PrimaryGeneratedColumn('uuid') on all entities as well.

e.g.

await queryRunner.query(`ALTER TABLE \`ring_actions\` DROP PRIMARY KEY`);
await queryRunner.query(`ALTER TABLE \`ring_actions\` DROP COLUMN \`id\``);
await queryRunner.query(`ALTER TABLE \`ring_actions\` ADD \`id\` varchar(36) NOT NULL PRIMARY KEY`);
await queryRunner.query(`ALTER TABLE \`order_items\` ADD CONSTRAINT \`FK_22866dffffb1fc32a384ecf3b0c\` FOREIGN KEY (\`cartId\`) REFERENCES \`orders\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`order_history\` ADD CONSTRAINT \`FK_464d9910b78618abc044b7aec7f\` FOREIGN KEY (\`cartId\`) REFERENCES \`orders\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`order_history_items\` ADD CONSTRAINT \`FK_e7c502e4d28c16d4247fb5c231e\` FOREIGN KEY (\`itemId\`) REFERENCES \`order_history\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
arminhupka

arminhupka commented on May 31, 2023

@arminhupka

I have the same issue with mysql2 v2 or v3 driver after version 0.3.10 (tried 0.3.12 and 0.3.16 issue exists in both). It tries to recreate ids and relations FKs etc.

We use @PrimaryGeneratedColumn('uuid') on all entities as well.

e.g.

await queryRunner.query(`ALTER TABLE \`ring_actions\` DROP PRIMARY KEY`);
await queryRunner.query(`ALTER TABLE \`ring_actions\` DROP COLUMN \`id\``);
await queryRunner.query(`ALTER TABLE \`ring_actions\` ADD \`id\` varchar(36) NOT NULL PRIMARY KEY`);
await queryRunner.query(`ALTER TABLE \`order_items\` ADD CONSTRAINT \`FK_22866dffffb1fc32a384ecf3b0c\` FOREIGN KEY (\`cartId\`) REFERENCES \`orders\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`order_history\` ADD CONSTRAINT \`FK_464d9910b78618abc044b7aec7f\` FOREIGN KEY (\`cartId\`) REFERENCES \`orders\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`order_history_items\` ADD CONSTRAINT \`FK_e7c502e4d28c16d4247fb5c231e\` FOREIGN KEY (\`itemId\`) REFERENCES \`order_history\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);

I had the same issue and I decided to update to @next version and everything is now is ok

ankitchhatbar

ankitchhatbar commented on Jun 3, 2023

@ankitchhatbar

The below works well for me

@Generated("uuid")
@PrimaryColumn({ length: 36 })
id: string
N-Andronopoulos

N-Andronopoulos commented on Jun 4, 2023

@N-Andronopoulos

Linked to #10090

pleerock

pleerock commented on Jun 20, 2023

@pleerock
Member

Must be fixed by #10041

sirmonin

sirmonin commented on Feb 26, 2024

@sirmonin

@pleerock this issue has been fixed for MySQL only. PostgreSQL is mentioned in the issue description by @Satont, and I can confirm that even 0.3.20 version still has this issue. Please, reopen this.

I use @PrimaryGeneratedColumn('uuid') and pgcrypto plugin

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

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

      migration:generate recreates defaults for no reason each migration · Issue #9750 · typeorm/typeorm