Skip to content

Postgres enum is recreated in every new generated migration #6471

@Nazar910

Description

@Nazar910

Issue type:

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

Database system/driver:

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

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.24 (or put your version here)

Steps to reproduce or a small repository showing the problem:
Hi, I have entity that uses enum as column type like this:

import {
    Entity, Column, PrimaryGeneratedColumn, Unique,
} from 'typeorm';

export enum CreationMechanism {
    SOURCE_A = 'SOURCE_A',
    SOURCE_B = 'SOURCE_B',
    SOURCE_C = 'SOURCE_C',
    SOURCE_D = 'SOURCE_D'
}

@Entity({ name: 'some_entity' })
@Unique([
    'field1',
    'field2',
])
export class SomeEntity {
    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    field1: string;

    @Column()
    field2: string;

    @Column({
        type     : 'enum',
        enumName : 'creation_mechanism_enum',
        enum     : CreationMechanism,
    })
    creationMechanism: CreationMechanism;

    @Column({ nullable: false, default: () => 'now()' })
    createdAt: Date;
}

Every time I want to generate new migration it always contains recreating of the previously defined enum. For example, I generated migration which contained recreating of an enum, and again executed migration generation, and now I have empty migration with the recreation of enum like this:

...
    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`ALTER TYPE "public"."creation_mechanism_enum" RENAME TO "creation_mechanism_enum_old"`, undefined);
        await queryRunner.query(`CREATE TYPE "creation_mechanism_enum" AS ENUM('SOURCE_A', 'SOURCE_B', 'SOURCE_C', 'SOURCE_D')`, undefined);
        await queryRunner.query(`ALTER TABLE "some_entity" ALTER COLUMN "creationMechanism" TYPE "creation_mechanism_enum" USING "creationMechanism"::"text"::"creation_mechanism_enum"`, undefined);
        await queryRunner.query(`DROP TYPE "creation_mechanism_enum_old"`, undefined);
    }
...

Please give me a hint if I'm doing something wrong. Thanks in advance.

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