pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

Types in array spread are not infered, e.g. in multiple update

Open technic opened this issue 3 years ago • 1 comments

I have a query like this

/* 
@name example
@params vals -> ((b, id)...)
*/
UPDATE table SET bar = v.bar FROM (VALUES :vals) as v(b, id) WHERE table.id = v.id

and pg asks me to cast v.id manually to integer, because pgtyped thinks id is a string.

technic avatar Jun 06 '22 19:06 technic

Just faced a very similar issue, it is partially due to how postgresql infer types in a value expression. ref: https://dba.stackexchange.com/a/228058

basically postgresql will interpret all values in a value expression to TEXT unless a type is explicitly defined.

using values in an insert is a special case, because postgresql can get the type from the inserting table.

probably as an improvement pgtyped can interpret type info in the magic comment like @params vals -> ((b::TEXT, id::INTEGER)...), but right now I think the only way to make it work is explicitly stating the type in SQL.

x-EricH-x avatar Aug 03 '22 21:08 x-EricH-x

This will be tracked in the meta issue. For now explicit type-casting is the only solution.

adelsz avatar Jan 28 '23 17:01 adelsz