How to insert an array using sails waterline into a postgresql table?

760 views Asked by At

I want to insert an array of strings in one column of a postgresql table using waterline ORM of sailsjs.

I have tried making the model Users.js like this :

 interest:{
      type: 'string',
      required: false,
      columnType: 'array'
    }

The insert query goes like this:

Users.create({ interest : ['programming'] });

The datatype in postgre table for interest column is character varying[].

When i try to perform an insert using this setup, it throws an error:

 Specified value (a array: [ 'programming' ]) doesn't match the expected type: 'string'

how can I insert an array in the postgresql table, how should the model look like?

Any help would be appreciated, thanks!

1

There are 1 answers

0
nahanil On BEST ANSWER

I use PG arrays quite a bit throughout my projects and have never had any issues using type: 'ref' then specifying a postgres array type in columnType. Something like the following:

 things: {
  type: 'ref',
  columnType: 'text[]',
  defaultsTo: null, // or defaultsTo: []
  description: 'An array of stringy-things',
},

PG array type docs: https://www.postgresql.org/docs/9.1/arrays.html, but basically you want probably want to use <TYPE>[] for your columnTypes, ie integer[], etc