3

I am attempting to insert a new row into a table with a column defined as an array or UUIDs:

alter table medias add column "order" uuid[];

I am using Objection.js ORM and attempting to execute the following query:

const order = [
  'BFAD6B0D-D3E6-4EB3-B3AB-108244A5DD7F'
]

Medias
  .query()
  .insert({
    order: lit(order.map(id => lit(id).castType('uuid'))).castArray()
  })

But the query is malformed and therefore does not execute:

INSERT INTO xxx ("order")
    VALUES (ARRAY [
      {"_value":"BFAD6B0D-D3E6-4EB3-B3AB-108244A5DD7F","_cast":"uuid","_toJson":false,"_toArray":false}
    ])

As can be seen, the query contains the JSON-stringified representation of the LiteralBuilder object and not something that the SQL syntax understands as a typecast.

If I skip casting the individual UUID strings and just cast the whole column into an array, then Postgres rejects the query because the column is of type uuid[] but I am attempting to insert the column as text[].

How can I format this query using Objection.js ORM?

My goal is to keep the column definition untouched and be able to insert a Postgres' array of UUIDs using Objection.js, either through its API or via raw query. If this is not currently possible with Objection, I am willing, as a last resort, to re-define the column as text[], but I would like to make sure I really have no other option.

Robert Rossmann
  • 11,931
  • 4
  • 42
  • 73

0 Answers0