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.

Postgres @Enum migration generating invalid syntax

See original GitHub issue

Describe the bug When modifying an existing string column to be an enum, the migration that gets generated seems to have a syntax error

To Reproduce Start with this table, generate and run the initial setup migration:

@Entity()
export class Report extends BaseEntity {
    @Property()
    status: string;
}

All good. Now change status to an enum:

export enum ReportStatus {
    PENDING = "PENDING",
    RUNNING = "RUNNING",
    FINISHED = "FINISHED",
    ERRORED = "ERRORED",
}

@Entity()
export class Report extends BaseEntity {
    @Enum()
    status: ReportStatus = ReportStatus.PENDING;
}

Creating a migration for this results in this:

export class Migration20200328220156 extends Migration {

  async up(): Promise<void> {
    this.addSql('alter table "report" alter column "status" drop default;');
    this.addSql('alter table "report" alter column "status" drop not null;');
    this.addSql('alter table "report" alter column "status" type text check ("status" in (\'PENDING\', \'RUNNING\', \'FINISHED\', \'ERRORED\')) using ("status"::text check ("status" in (\'PENDING\', \'RUNNING\', \'FINISHED\', \'ERRORED\')));');
    this.addSql('alter table "report" alter column "status" set not null;');
  }
}

And executing that gives a syntax error:

$ /Users/js/Documents/.../node_modules/.bin/mikro-orm migration:up
== Migration20200328220156: migrating =======
mikro-orm migration:up

Migrate up to the latest version

Options:
  -t, --to       Migrate up to specific version                         [string]
  -f, --from     Start migration from specific version                  [string]
  -o, --only     Migrate only specified versions                        [string]
  -v, --version  Show version number                                   [boolean]
  -h, --help     Show help                                             [boolean]

error: alter table "report" alter column "status" type text check ("status" in ('PENDING', 'RUNNING', 'FINISHED', 'ERRORED')) using ("status"::text check ("status" in ('PENDING', 'RUNNING', 'FINISHED', 'ERRORED'))); - syntax error at or near "check"

The problem seems to be this line

alter table "report" alter column "status" type text check ("status" in (\'PENDING\', \'RUNNING\', \'FINISHED\', \'ERRORED\')) using ("status"::text check ("status" in (\'PENDING\', \'RUNNING\', \'FINISHED\', \'ERRORED\')));

If I drop my db and get rid of the initial migration, the create table statement gets generated and run correctly, so that’s a fairly easy workaround for the moment.

Additional info

My mikro-orm.config.ts:

const config: Options<IDatabaseDriver<Connection>> = {
    entities: [BaseEntity, Report, Reporter, ReportLog],
    entitiesDirsTs: ["src"],
    type: "postgresql",
    host: "localhost",
    dbName: "reports",
};

P.S Sorry for making so many issues 😃

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
willsotocommented, Apr 26, 2021

@B4nan sorry to necro an old issue, but I wanted to point out that Knex does support native enums on postgres. See the docs: https://knexjs.org/#Schema-enum

0reactions
B4nancommented, Apr 26, 2021

I know it does, but we would have to implement all the schema diffing too (which might not be that hard, but still its much more work than using a bool flag in knex).

Open for PR if it’s that important for somebody, personally the text columns with check constraints seem to be much better solution, but havent really used the native ones in the wild, so I might be wrong.

Read more comments on GitHub >

github_iconTop Results From Across the Web

sqlalchemy postgresql enum does not create type on db migrate
I have a problem with postgresql enum type on db migrate/upgrade. I added a column "status" to model: class Banner(db.
Read more >
Documentation: 15: 8.7. Enumerated Types - PostgreSQL
Enumerated (enum) types are data types that comprise a static, ordered set of values. ... Enum types are created using the CREATE TYPE...
Read more >
Dealing with Enum Type in PostgreSQL - DEV Community ‍ ‍
The workaround is the same as above: rename old type, create new and correct type, and delete old type.
Read more >
Upgrading PostgreSQL's Enum type with SQLAlchemy using ...
In this tutorial we've successfully generated the migration file which can automatically upgrade enum types on PostgreSQL server.
Read more >
Database: Migrations - The PHP Framework For Web Artisans
You may use the make:migration Artisan command to generate a database migration. ... Migration squashing is only available for the MySQL, PostgreSQL, ...
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