Alberto Méndez
Alberto Méndez

Reputation: 1064

Default value in Knex migration for an specific type of text array not working

I am creating a database migration using Knex (v0.19.5) and PostgreSQL (v10.1) but when I try to set the default value to a TEXT array column it gives me a malformed array literal error.

table.specificType('test', 'TEXT[]').defaultTo(['foo', 'bar']);

This is the error message

Array value must start with "{" or dimension information.

error: malformed array literal: "foo,bar"

Maybe I am missing something but I can't get it to work and I can't find anything useful in their official docs.

Upvotes: 2

Views: 3744

Answers (2)

Kevin Potgieter
Kevin Potgieter

Reputation: 798

Another solution is to use pg.raw('...')

table.specificType("test", "text[]").defaultTo(pg.raw(`'{}'::text[]`))

Upvotes: 0

Alberto Méndez
Alberto Méndez

Reputation: 1064

I finally solved it by simply setting the array into a literal string.

table.specificType('test', 'TEXT[]').defaultTo('{\'\'foo\'\',\'\'bar\'\'}');

Upvotes: 5

Related Questions