Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need foreign key as array [duplicate]

Tags:

postgresql

CREATE TABLE test ( id int PRIMARY KEY , name );

CREATE TABLE test1 ( id integer[] REFERENCES test , rollid int );

ERROR: foreign key constraint "test3_id_fkey" cannot be implemented DETAIL: Key columns "id" and "id" are of incompatible types: integer[] and integer.

after that I try to another way also

CREATE TABLE test1 ( id integer[] , rollid int);

ALTER TABLE test1 ADD CONSTRAINT foreignkeyarray FOREIGN KEY (id) REFERENCES test;

ERROR: foreign key constraint "fkarray" cannot be implemented DETAIL: Key columns "id" and "id" are of incompatible types: integer[] and integer.

so I try create a foreign key array means it say error. please tell me anyone?

postgresql version is 9.1.

like image 602
vara Avatar asked Feb 06 '26 21:02

vara


1 Answers

What you're trying to do simply can't be done. At all. No ifs, no buts.

Create a new table, test1_test, containing two fields, test1_id, test_id. Put the foreign keys as needed on that one, and make test1's id an integer.

like image 135
Denis de Bernardy Avatar answered Feb 09 '26 11:02

Denis de Bernardy