Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"invalid input syntax for type json" when trying to store an array of objects in PG jsonb column #4548

Closed
bill-o-more opened this issue Jul 23, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@bill-o-more
Copy link

Describe the bug
I'm trying to store an array of objects inside a jsonb column in Postgres.
My model is defined like this:

type GeoItem = {
    kind: 'borough' | 'city' | 'county' | 'etc';
    name: string
}
export class ServicePerson {
    @PrimaryKey({ columnType: 'uuid', defaultRaw: `uuid_generate_v4()` })
    id = uuidv4();

    @Property({ columnType: 'jsonb', default: [] })
    serviceArea: GeoItem[] = [];
}

When I run

const servicePerson = orm.em.create(ServicePerson, { serviceArea: [{kind: 'city', name: 'Mordor'}, {kind: 'city', name: 'Gotham'}] });
orm.em.persistAndFlush(servicePerson);

I get the following error:

"insert into \"service_person\" (\"id\", \"service_area\") values ('21a101b6-cae5-4094-bbf7-5a19eef37d7f', '{[object Object],[object Object]}') returning \"id\", \"service_area\" - invalid input syntax for type json"

No TS warnings whatsoever, if I make a breakpoint just before flushing and inspect theservicePerson entity I see the correct array with correct objects.

Stack trace

{"type":"DriverException","message":"insert into "service_person" ("id", "service_area") values ('21a101b6-cae5-4094-bbf7-5a19eef37d7f', '{[object Object],[object Object]}') returning "id", "service_area" - invalid input syntax for type json" - invalid input syntax for type json","stack":"DriverException: insert into "service_person" ("id", "service_area") values ('21a101b6-cae5-4094-bbf7-5a19eef37d7f', '{[object Object],[object Object]}') returning "id", "service_area - invalid input syntax for type json
    at PostgreSqlExceptionConverter.convertException (/path/to/my/project/node_modules/@mikro-orm/core/platforms/ExceptionConverter.js:8:16)
    at PostgreSqlExceptionConverter.convertException (/path/to/my/project/node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:42:22)
    at PostgreSqlDriver.convertException (/path/to/my/project/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:197:54)
    at /path/to/my/project/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:201:24
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async PostgreSqlDriver.nativeInsertMany (/path/to/my/project/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:303:21)
    at async ChangeSetPersister.persistNewEntity (/path/to/my/project/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:85:21)
    at async ChangeSetPersister.executeInserts (/path/to/my/project/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:29:13)
    at async ChangeSetPersister.runForEachSchema (/path/to/my/project/node_modules/@mikro-orm/core/unit-of-work/ChangeSetPersister.js:68:13)
    at async UnitOfWork.commitCreateChangeSets (/path/to/my/project/node_modules/@mikro-orm/core/unit-of-work/UnitOfWork.js:739:9)

Expected behavior
I'd expect this array to be saved into the DB with no errors

Additional context
I'm a noob. Please point me into the relevant doc if I missed one - seen this, but couldn't find the answer there

Versions

Dependency Version
node 18.12.1
typescript 5.0.4
mikro-orm 5.7.12
your-driver Postgres
@B4nan
Copy link
Member

B4nan commented Jul 24, 2023

Looks like the detection of JSON types based on columnType is not working properly.

You should be doing this instead, as JSONb is the default mapping for JSON columns in postgres, no need to specify that via columnType. By using type you will also set the JsonType to that property which handles the conversion.

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

edit: I see, the problem is that this is array mapping, and the mapped type is first inferred as ArrayType instead, should be easy to fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants