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

Do not create duplicate CREATE TYPE migration query when same 'enumName's are exists #7523

Open
2 of 21 tasks
5d-jh opened this issue Apr 1, 2021 · 8 comments
Open
2 of 21 tasks

Comments

@5d-jh
Copy link

5d-jh commented Apr 1, 2021

Feature Description

The Problem

When there's a parent entity class with enum field that has multiple children, typeorm migration creates duplicate CREATE TYPE query in migration file. This causes Type already defined error when running migration.

For example,

// foobar-entity.ts
enum FooEnum { FOO, BAR }

class ParentEntity {
  @Column({
    type: 'enum',
    enum: FooEnum,
    enumName: 'foo_enum',
  })
  foo: FooEnum;
}

@Entity()
class ChildEntity1 extends ParentEntity {}

@Entity()
class ChildEntity2 extends ParentEntity {}

Then generate migration,

// TIMESTAMP-FoobarMigration.ts
export class FoobarMigrationTIMESTAMP implements MigrationInterface {
  name = 'FoobarMigrationTIMESTAMP';

  public async up(queryRunner: QueryRunner) {
    await queryRunner.query('CREATE TYPE "foo_enum" AS ENUM ~~');
    // (create table child entity 1)
    await queryRunner.query('CREATE TYPE "foo_enum" AS ENUM ~~');
    // (create table child entity 2)
  }
}

The Solution

Storing enumName in set or map to prevent from generating duplicate query might solve the issue.

Considered Alternatives

For now, as of my knowledge, removing duplicate queries manually is the only way to solve the issue.

Additional Context

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@AlexMesser
Copy link
Collaborator

This bug was fixed in #7419 and released in 0.2.32. Please update your TypeORM version and reopen this issue if the error persists.

@5d-jh
Copy link
Author

5d-jh commented Apr 2, 2021

I updated typeorm to 0.2.32 but the issue seems to be still exists.

@AlexMesser
Copy link
Collaborator

ok, I'll add a test for this case

@AlexMesser AlexMesser reopened this Apr 2, 2021
AlexMesser added a commit that referenced this issue Apr 3, 2021
5d-jh added a commit to 5d-jh/typeorm that referenced this issue Apr 12, 2021
Store built enum names in map class property

Closes: typeorm#7523
5d-jh added a commit to 5d-jh/typeorm that referenced this issue Apr 12, 2021
Store built enum names in map class property

Closes: typeorm#7523
@jghowe
Copy link

jghowe commented May 14, 2021

I am seeing this same issue and it results in failures when synchronize: true is turned on when using enumName.

@MrBacony
Copy link

what about the open pull request.... are there some concerns?

@opunbuds
Copy link

I have same issue too, any update on this guys ?

@Nightbr
Copy link

Nightbr commented Jan 4, 2023

Same issue here too, it create n times the enum type the first time. If you edit the migration file and run the migration the next generate doesn't recreate enum type.

@jhonland
Copy link

jhonland commented Feb 3, 2023

Having the same issue here, using typeorm 0.3.11.
I have an enum Status and I'm referencing it in two tables: TableA and TableB, but typeorm always tries to generate a migration for it twice, even when setting @Column({ enumName: 'my_enum' })

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

Successfully merging a pull request may close this issue.

8 participants