Postgres @Enum migration generating invalid syntax
See original GitHub issueDescribe 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:
- Created 3 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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
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.