mikro-orm: Error while storing array of string enums into jsonb column

Describe the bug I have an entity defined as

enum GreatEnum {
  EnumValue = 'enumValue',
  AnotherEnumValue = 'anotherEnumValue'
}
export class MyEntity {
    @PrimaryKey({ columnType: 'uuid', defaultRaw: `uuid_generate_v4()` })
    id = uuidv4();

    @Property({ type: 'json', default: '[]' })
    greatProp: GreatEnum[] = [];
}

Two things happen. First - migration generator detects it as text[] instead of jsonb. Second - when I manually correct the migration and jsonb column is created in DB, inserts are broken - this is the query I get:

insert into "my_entity" ("id", "great_prop")
values ('some-long-guid', '{enumValue,anotherEnumValue}') 
returning.....

Which surely throws invalid input syntax for type json. Stack trace

insert into \"my_entity\" (\"id\", \"great_prop\") values ('11fdce9e-8e89-4e4f-a244-431a9ada814a',  '{enumValue,anotherEnumValue}' - invalid input syntax for type json\n    at Parser.parseErrorMessage (/path/to/my/project/node_modules/pg-protocol/src/parser.ts:369:69)\n    at Parser.handlePacket (/path/to/my/project/node_modules/pg-protocol/src/parser.ts:188:21)\n    at Parser.parse (/path/to/my/project/node_modules/pg-protocol/src/parser.ts:103:30)\n    at Socket.<anonymous> (/path/to/my/project/node_modules/pg-protocol/src/index.ts:7:48)\n    at Socket.emit (node:events:513:28)\n    at Socket.emit (node:domain:489:12)\n    at addChunk (node:internal/streams/readable:324:12)\n    at readableAddChunk (node:internal/streams/readable:297:9)\n    at Socket.Readable.push (node:internal/streams/readable:234:10)\n    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)","length":184,"name":"DriverException","severity":"ERROR","code":"22P02","detail":"Token \"enumValue\" is invalid.","]

Expected behavior I’d expect it to insert an array of strings with correct json syntax

Additional context The data arrives to create correctly, no type errors exist

Versions

Dependency Version
node 18.12.1
typescript 5.0.4
mikro-orm 5.7.12
your-driver Postgres

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (10 by maintainers)

Commits related to this issue

Most upvoted comments

But from the code I can see how this would be still happening, hmm weird I can’t reproduce it.

Will fix that in a minute.

Dude, it was you reporting it? 😄

#4548