question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Columns with the type "enum" are not supported in the migration API

See original GitHub issue

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:

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

Steps to reproduce or a small repository showing the problem:

When writing migrations by hand using the migration API to create a new table that has an enum column 3 things happen that shouldn’t:

  • The generated SQL does not sets the column’s type to enum.
  • Worse, it sets the column’s type to “[Tablename]_[column]_enum”.
  • The “enum” TableColumnOptions parameter gets ignored.

A simple example:

// == The entity

export enum RatingType {
  low = 'bad',
  neutral = 'neutral',
  good = 'good'
}

@Entity({ name: 'Ratings' })
export default class Rating extends BaseEntity {
  @PrimaryGeneratedColumn('uuid')
  id: string

  @Column({ type: 'enum', enum: RatingType })
  rating: RatingType
}

// == The migration

import { MigrationInterface, QueryRunner, Table } from 'typeorm'

export class CreateRatings1569818629816 implements MigrationInterface {
  private ratingsTable = new Table({
    name: 'Ratings',
    columns: [
      {
        name: 'id',
        type: 'uuid',
        isPrimary: true,
        isGenerated: true,
        generationStrategy: 'uuid',
      },
      {
        name: 'rating',
        type: 'enum',
        enum: ['bad', 'neutral', 'good'],
        isNullable: true
      }
    ],
  })

  async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.createTable(this.ratingsTable, true)
  }

  async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.dropTable(this.ratingsTable)
  }
}

The generated SQL looks like this:

CREATE TABLE "Ratings" (
  "id" uuid NOT NULL DEFAULT uuid_generate_v4(),
  "rating" "Ratings_rating_enum",
  CONSTRAINT "PK_ee6436ff188c9bb00cc70fc447a" PRIMARY KEY ("id")
)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

25reactions
ralmarri12commented, Jun 21, 2020

how to set default value for enum

this worked with me

{
    name: 'status',
    type: 'enum',
    enum: ['active', 'inactive', 'paused'],
    enumName: 'statusEnum',
    default: '"inactive"'
}
19reactions
bigLucascommented, May 26, 2021

how to set default value for enum

this worked with me

{
    name: 'status',
    type: 'enum',
    enum: ['active', 'inactive', 'paused'],
    enumName: 'statusEnum',
    default: '"inactive"'
}

Almost the same solution, but here with typeorm 0.2.30 and Postgres as database, I fixed with this:

// enum
enum NiceEnum {
    FOO: 'FOO',
    BAR: 'BAR',
}
// column
{
    name: 'status',
    type: 'enum',
    enum: [NiceEnum.FOO, NiceEnum.BAR],
    enumName: 'statusEnum',
    default: `'${NiceEnum.FOO}'` // here, I used single quotes
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Postgres enum in TypeORM - Stack Overflow
enum column type is supported by postgres and mysql. There are various possible column definitions: Using typescript enums:.
Read more >
Bug? Changing a column of table that has an enum type.
Hi, I'm trying to make a new migration for updating a table: Originally, it has: id, name, gender Gender is an ENUM, name...
Read more >
MySQL 8.0 Reference Manual :: 11.3.5 The ENUM Type
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column...
Read more >
Documentation: 15: 8.7. Enumerated Types - PostgreSQL
Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in...
Read more >
Prisma schema API (Reference)
Unsupported fields can be introspected with prisma db pull or created with Prisma Migrate or db push . Remarks. Fields with Unsupported types...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found