将数组插入PostgreSQL [英] Inserting array into postgresql

查看:89
本文介绍了将数组插入PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文件中插入一个ID数组,但是在创建表时我使用了 int ,它现在具有另一个表的引用,我该如何存储数组值到特定字段?
我知道这些列必须是同一类型,并且我还看到有些人已经尝试解决数组问题上的外键:
我也尝试了ELEMENT根据此文档,但没有成功。请有人在这里帮助我 https:// blog。 2ndquadrant.com/postgresql-9-3-development-array-element-foreign-keys/

I want to insert an array of id's to a file, but while creating a table I used int, and it has the reference of another table now how can I store array value to the specific field? I understand that the columns have to be the same type and I also saw that some tried to tackle this foreign key on array issue already: also i tried ELEMENT according to this doc but no success. please someone help me here https://blog.2ndquadrant.com/postgresql-9-3-development-array-element-foreign-keys/

db.query('CREATE TABLE IF NOT EXISTS departments_users(_id SERIAL NOT NULL UNIQUE PRIMARY KEY,user_id int REFERENCES companyUser ON DELETE CASCADE,department_id int REFERENCES departments ON DELETE CASCADE)');

exports.addCompanyUser = function(data, callback) {
    db.query('INSERT INTO departments_users(user_id,department_id) VALUES($1,$2) RETURNING *', [companyuseraccess.rows[0]._id, data.department]).then(function(departments_users) {      
    })
}


推荐答案

您没有任何具有 ARRAY dataType的字段,因此从本质上讲您不能将数组插入该字段。如果您希望将数组放入字段中,请先将其数据类型更改为 ARRAY ,然后可以

You dont have any field with ARRAY dataType so essentially you cannot insert array into the field. If you wish to put in array in a field change its data type to ARRAY first and then you can

INSERT INTO "some_table" ("id","array_field","some_text_field") 
VALUES (1,ARRAY['value1','value2']::TEXT[],'active') RETURNING *

希望这会有所帮助。让我知道我能否进一步帮助

Hope this helps. Let me know if i can help further

这篇关于将数组插入PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆