KyselyK
Kysely3y ago
19 replies
Theo

Using Postgres function in an insert with a field as a select

I'm trying to translate the following SQL:

INSERT INTO sb.text_document_chunk (text_document_id, text)
SELECT NEW.id, sentence
FROM unnest(string_to_array(NEW.text_content, '. ')) AS sentence
WHERE sentence ~ '[[:alnum:]]';

to:
db.insertInto('text_document_chunk').values((eb) => ({
    text_document_id: document.id,
    text: db
      .selectFrom(eb.fn('unnest', [eb.fn('string_to_array', [document.text_content, '. '])]).as('sentence'))
      .where('sentence', '~', '[[:alnum:]]'),
  })).execute()

It doesn't seem to like
[document.text_content, '. ']
and
where('sentence')
. It seems the array value has to be a
ReferenceExpression
type, but the documentation isn't very clear on what that is.
Was this page helpful?