typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.97k stars 6.26k forks source link

enumName causes duplication of global enums #8136

Open Goldziher opened 3 years ago

Goldziher commented 3 years ago

Issue Description

When using the enumName parameter to define global enums, TypeORM creates multiple instances of the global enum, causing errors.

Expected Behavior

Only a single global enum is created per type, if the enumName property is the same

Actual Behavior

This is copied from the migration generated, i ordered it alphabetically so its clearer:

public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TYPE "AggregationType" AS ENUM('PLAYOFFS_KNOCK_OUT', 'PLAYOFFS', 'SEASON', 'STAGE')`);
        await queryRunner.query(`CREATE TYPE "CompetitionType" AS ENUM('BBL', 'BBL_CUP', 'ALLSTAR', 'CHAMPIONS_CUP')`);
        await queryRunner.query(`CREATE TYPE "CompetitionType" AS ENUM('BBL', 'BBL_CUP', 'ALLSTAR', 'CHAMPIONS_CUP')`);
        await queryRunner.query(`CREATE TYPE "CompetitionType" AS ENUM('BBL', 'BBL_CUP', 'ALLSTAR', 'CHAMPIONS_CUP')`);
        await queryRunner.query(`CREATE TYPE "CompetitionType" AS ENUM('BBL', 'BBL_CUP', 'ALLSTAR', 'CHAMPIONS_CUP')`);
        await queryRunner.query(`CREATE TYPE "GameOutcomeType" AS ENUM('W', 'L')`);
        await queryRunner.query(`CREATE TYPE "GroupType" AS ENUM('NORTH', 'SOUTH', 'GROUP_1', 'GROUP_2', 'GROUP_3', 'GROUP_4', 'GROUP_A', 'GROUP_B', 'GROUP_C', 'GROUP_D')`);
        await queryRunner.query(`CREATE TYPE "GroupType" AS ENUM('NORTH', 'SOUTH', 'GROUP_1', 'GROUP_2', 'GROUP_3', 'GROUP_4', 'GROUP_A', 'GROUP_B', 'GROUP_C', 'GROUP_D')`);
        await queryRunner.query(`CREATE TYPE "ImportType" AS ENUM('Referee', 'Agent', 'Team', 'Player', 'Coach', 'Venue', 'Game', 'CoachComment', 'Champions', 'Standing', 'TeamSeasonStat', 'TeamGameStat', 'PlayerSeasonStat', 'PlayerGameStat', 'CoachGameStat', 'Penalties', 'PlayerTransfer')`);
        await queryRunner.query(`CREATE TYPE "PlayerBestStatisticType" AS ENUM('points', 'twoPointShotsMade', 'twoPointShotsAttempted', 'threePointShotsMade', 'threePointShotsAttempted', 'freeThrowsMade', 'freeThrowsAttempted', 'offensiveRebounds', 'defensiveRebounds', 'totalRebounds', 'assists', 'steals', 'blocks', 'secondsPlayed')`);
        await queryRunner.query(`CREATE TYPE "PositionType" AS ENUM('CENTER', 'POWER_FORWARD', 'POINT_GUARD', 'SMALL_FORWARD', 'SHOOTING_GUARD')`);
        await queryRunner.query(`CREATE TYPE "SeasonStatType" AS ENUM('TEAM', 'OPPONENT')`);
        await queryRunner.query(`CREATE TYPE "StageType" AS ENUM('MAIN_ROUND', 'ROUND_OF_16', 'ROUND_OF_8', 'SEMI_FINALS', 'FINALS', 'THIRD_PLACE_GAME', 'QUALIFICATION', 'GROUP_STAGE')`);
        await queryRunner.query(`CREATE TYPE "StageType" AS ENUM('MAIN_ROUND', 'ROUND_OF_16', 'ROUND_OF_8', 'SEMI_FINALS', 'FINALS', 'THIRD_PLACE_GAME', 'QUALIFICATION', 'GROUP_STAGE')`);
        await queryRunner.query(`CREATE TYPE "StageType" AS ENUM('MAIN_ROUND', 'ROUND_OF_16', 'ROUND_OF_8', 'SEMI_FINALS', 'FINALS', 'THIRD_PLACE_GAME', 'QUALIFICATION', 'GROUP_STAGE')`);
        await queryRunner.query(`CREATE TYPE "StageType" AS ENUM('MAIN_ROUND', 'ROUND_OF_16', 'ROUND_OF_8', 'SEMI_FINALS', 'FINALS', 'THIRD_PLACE_GAME', 'QUALIFICATION', 'GROUP_STAGE')`);
        await queryRunner.query(`CREATE TYPE "TelevisionType" AS ENUM('none', 'live', 'recording', 'highLights', 'timeShifted')`);
  //...
}

Steps to Reproduce

  1. create two columns that use the same global enum, with the same enumName property
  2. create a migration
// insert code here

My Environment

Dependency Version
Operating System
Node.js version x.y.zzz
Typescript version x.y.zzz
TypeORM version x.y.zzz

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 yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

fiendfiend commented 2 years ago

This was fixed at one point but seems to have regressed: https://github.com/typeorm/typeorm/issues/7647

rolaru commented 1 year ago

Happens for me as well, although I have the exact same enumName in two different entities:

@Column({
  name: "apps_visibility",
  nullable: true,
  type: "enum",
  enum: AppsVisibility,
  enumName: 'AppsVisibilityEnum',
  array: true,
  default: [AppsVisibility.ONBOARDING],
})
@Field()
appsVisibility: AppsVisibility[];
Distortedlogic commented 9 months ago

same issue for me

thenglong commented 8 months ago

Just face the same issue!

MAG1980 commented 4 months ago

I had the same problem. When generating a migration, TypeORM creates the same Enum several times under different names. As a result, an error occurs when performing the migration, because The data types of related columns in different tables are not the same (Enums have different names).