如何使用sails水线将数组插入postgresql表?

问题描述 投票:2回答:1

我想使用sailsjs的水线ORM在postgresql表的一列中插入一个字符串数组。

我试过像这样制作模型Users.js

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

insert查询如下所示:

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

postgre表中感兴趣的列的数据类型是character varying[]

当我尝试使用此设置执行插入时,它会抛出一个错误:

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

如何在postgresql表中插入数组,模型应该如何?任何帮助,将不胜感激。

postgresql sails.js waterline sails-postgresql
1个回答
1
投票

我在整个项目中使用了PG数组,并且在使用type: 'ref'然后在columnType中指定postgres数组类型时从未遇到过任何问题。类似于以下内容:

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

PG数组类型docs:https://www.postgresql.org/docs/9.1/arrays.html,但基本上你想要使用<TYPE>[]作为你的columnTypes,即integer[]

© www.soinside.com 2019 - 2024. All rights reserved.