var query=`UPDATE "public"."vehicle_ads"
SET options=
jsonb_set(
options,
'{$1, ${subgroup}, -1}',
'{"name": "${optionName}"}'::jsonb
)
WHERE "vehicleId"=${id}`;
I'm having the following issues with the above query. If I replace $1 with ${group}, no error. But if I leave $1 and pass group as a prepared statement, I get the error...
bind message supplies 1 parameters, but prepared statement "" requires 0
My goal is to tokenize the entire query, e.g.:
var query=`UPDATE "public"."vehicle_ads"
SET options=
jsonb_set(
options,
'{$1, $2, -1}',
'{"name": $3}'::jsonb
)
WHERE "vehicleId"=$4`;
Then...
could not determine data type of parameter $1
I know I'm lost in a formatting soup of template strings and Postgres, unsure what needs ticks, single quotes, or double. Any help is greatly appreciated.
EDIT:
Here's what I'm doing with detail.
I have a nested object with vehicle options data stored as a jsonb field. It has the form:
{
"Group": {
"SubGroup": [
{
"name": string,
"hasOption": bool
}
...
]
...
}
...
}
I want to edit the name in a sub group. For instance Powertrain.Drivetrain[0].name='AWD';
jsonb_set(options, ARRAY[$1, $2, '-1', 'name']::text[], $3::jsonb)Error: invalid input syntax for type json. The first argument of jsonb_set is a jsonb object, second is a text path, third is the new value in jsonb. I triedjsonb_set(options, '{' || ARRAY[$1,$2,'-1']::text[] || '}', '{"name": $3}'::jsonbbut that returnsError: malformed array literal: "{".nameproperty, is not a valid json(b). You could try to convert to json(b) withto_json($3)orto_jsonb($3), but it would be helpful to include the parameter types (and perhaps example values for the parameters) in the question.nameproperty to the text path, because placeholders for parameters won't work in any literals (so'{"name": $3}'::jsonbalso won't work)jsonb_set(options, ARRAY[$1, $2, $3, $4], to_json($5)::jsonb)and it returned error messagecould not determine polymorphic type because input has type "unknown".jsonb_set(options, ARRAY[$1, $2, $3, $4], "${value}"'::jsonb)works perfectly