KyselyK
Kysely3y ago
15 replies
significantotter

Are JSON Arrays not supported for paramterization in the postgres driver?

Hey! I've been loving kysely so far and doing a ton of awesome code cleanup and refactoring with it. But I just hit a big snag with a seeming inability to use my json array columns for updates. It seems that the parameter parsing doesn't work with JSON.

The types seem fine:

https://kyse.link/?p=s&i=pc0iBgUrhjXxfAlSEh9i

(Note that my actual app has well-defined types instead of the object primitive types used in the example)

But then when I run this, I get

{
severity: 'ERROR',
code: '22P02',
detail: 'Expected ":", but found "}".',
where: 'JSON data, line 1: {"{\"bar\":\"baz\"}"}\n' +
"unnamed portal parameter $2 = '...'",
file: 'jsonfuncs.c',
line: '621',
routine: 'json_ereport_error'
}

The only other question I could find related to this in here had a suggestion to stringify the values. But then it breaks the column interface typing and I have to lie about the type every time I use it. That doesn't seem right to me.

One other minor, interesting note. The update with
[]
works, it only begins to fail if you try to update
[{}]
.
Solution
Yeah, the pg driver doesn't serialize arrays. I think the reasoning is that it doesn't know, when serializing, if the target column will be a postgres array or json.

If you only make the update and insert types
string
, everything works correctly. The row type is inferred correctly for output data.

https://kyse.link/?p=s&i=NBfnbfzwNBlZ0YJpc7dj
Was this page helpful?