我正在讨论这个问题,我的时间不多了,所以请大家帮忙:我想插入以下数据:
const data= {
id:user.id,
choice:'SWOT',
label:['Strengths','Weaknesses','Opportunities','Threats'],
results:[45,5,20,30],
description:'My first Strategic Analysis'}
放入此表:
analyses (
id serial primary key,
userID integer not null,
choice varchar(25) not null,
Label text ARRAY,
Results integer ARRAY,
description varchar(200),
FOREIGN KEY (userID) REFERENCES users (id)
);
使用knex,这应该类似于smth:
db('analyses').insert({
userid: data.id,
choice: data.choice,
Label: data.labelG,
Results: data.resultG,
description: data.description
})
由于这种语法对数组类型不起作用,我想知道如何做到这一点?有些人建议使用knex.raw(),但是我没有得到正确的语法,有什么帮助吗?
您可以直接将javascript数组传递给array
类型的列。像这样:
await knex.schema.createTable('foo', t => {
t.increments('id');
t.specificType('intarray', 'integer ARRAY');
t.specificType('stringarray', 'text ARRAY');
});
await knex('foo').insert({ intarray: [4,3,2,1], stringarray: ['foo','bar'] });
const rows = await knex('foo');
console.log(rows);
// should output:
// [ anonymous { id: 1, intarray: [ 4,3,2,1 ], stringarray: [ 'foo', 'bar' ] } ]