postgresql 外键整数[] 指的是整数
postgresql foreign key integer[] refers to integer
我正在处理 postgresql 数据库,我遇到了一个问题:
我想创建一个包含 categories_ids 列的 table 申请人(这意味着申请人可以有多个类别)并且我想在此列和列 id 之间创建一个外键约束类别 table。但是 pgadmin 说那是不可能的:
foreign key constraint "fk_categories_ids" cannot be implemented
DÉTAIL : Key columns "categories_ids" and "id" are of incompatible types: integer[] and integer.
编辑 1:
CREATE TABLE public.applicants
(
-- Hérité(e) from table users: id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
-- Hérité(e) from table users: email character(60) NOT NULL,
-- Hérité(e) from table users: "firstName" character(50) NOT NULL,
-- Hérité(e) from table users: "lastName" character(50) NOT NULL,
-- Hérité(e) from table users: password character(50) NOT NULL,
-- Hérité(e) from table users: role_id integer NOT NULL DEFAULT nextval('users_role_id_seq'::regclass),
home boolean NOT NULL,
"fullTime" boolean,
"partTime" boolean NOT NULL,
freelance boolean NOT NULL,
internship boolean NOT NULL,
"minSalary" integer,
domain_id integer,
categories_ids integer[],
skills_ids integer[],
locations_ids integer[],
"jobExperiences_ids" integer[],
CONSTRAINT pk_applicant_id PRIMARY KEY (id),
CONSTRAINT fk_domain_id FOREIGN KEY (domain_id)
REFERENCES public.domains (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
INHERITS (public.users)
WITH (
OIDS=FALSE
);
删除 categories_ids integer[]
字段并:
create table applicant_category (
applicant_id int references applicants(id),
category_id int references category(category_id),
primary key (applicant_id, category_id)
)
您不能在数组列上创建 FK。不支持。
所以要么使用 EAV 模型(https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model,所以 3 个表而不是 2 个),或者在没有外键的情况下工作(不过你可以创建自定义触发器来模拟它们)。
有一些文章将 EAV 与使用 "non-primitive"(int[]
、hstore
、jsonb
)数据类型进行比较——例如 http://coussej.github.io/2016/01/14/Replacing-EAV-with-JSONB-in-PostgreSQL/ and https://wiki.hsr.ch/Datenbanken/files/Benchmark_of_KVP_vs.hstore-_doc.pdf 有一些基准。
我正在处理 postgresql 数据库,我遇到了一个问题:
我想创建一个包含 categories_ids 列的 table 申请人(这意味着申请人可以有多个类别)并且我想在此列和列 id 之间创建一个外键约束类别 table。但是 pgadmin 说那是不可能的:
foreign key constraint "fk_categories_ids" cannot be implemented
DÉTAIL : Key columns "categories_ids" and "id" are of incompatible types: integer[] and integer.
编辑 1:
CREATE TABLE public.applicants
(
-- Hérité(e) from table users: id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),
-- Hérité(e) from table users: email character(60) NOT NULL,
-- Hérité(e) from table users: "firstName" character(50) NOT NULL,
-- Hérité(e) from table users: "lastName" character(50) NOT NULL,
-- Hérité(e) from table users: password character(50) NOT NULL,
-- Hérité(e) from table users: role_id integer NOT NULL DEFAULT nextval('users_role_id_seq'::regclass),
home boolean NOT NULL,
"fullTime" boolean,
"partTime" boolean NOT NULL,
freelance boolean NOT NULL,
internship boolean NOT NULL,
"minSalary" integer,
domain_id integer,
categories_ids integer[],
skills_ids integer[],
locations_ids integer[],
"jobExperiences_ids" integer[],
CONSTRAINT pk_applicant_id PRIMARY KEY (id),
CONSTRAINT fk_domain_id FOREIGN KEY (domain_id)
REFERENCES public.domains (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
INHERITS (public.users)
WITH (
OIDS=FALSE
);
删除 categories_ids integer[]
字段并:
create table applicant_category (
applicant_id int references applicants(id),
category_id int references category(category_id),
primary key (applicant_id, category_id)
)
您不能在数组列上创建 FK。不支持。 所以要么使用 EAV 模型(https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model,所以 3 个表而不是 2 个),或者在没有外键的情况下工作(不过你可以创建自定义触发器来模拟它们)。
有一些文章将 EAV 与使用 "non-primitive"(int[]
、hstore
、jsonb
)数据类型进行比较——例如 http://coussej.github.io/2016/01/14/Replacing-EAV-with-JSONB-in-PostgreSQL/ and https://wiki.hsr.ch/Datenbanken/files/Benchmark_of_KVP_vs.hstore-_doc.pdf 有一些基准。