postgresql外键整数[]指整数

时间:2017-06-29 09:11:08

标签: postgresql foreign-key-relationship

我正在使用postgresql数据库,我遇到了一个问题:

我想创建一个包含列categories_ids的表申请人(这意味着申请人可以有多个类别),并且我希望在此列和类别列的列ID之间创建外键约束。但是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
);

2 个答案:

答案 0 :(得分:0)

删除import numpy as np import matplotlib.pyplot as plt def two_scales(ax1, time, data1, data2, c1, c2): ax2 = ax1.twinx() ax1.plot(time, data1, color=c1) ax1.set_xlabel('time (s)') ax1.set_ylabel('exp') ax2.plot(time, data2, color=c2) ax2.set_ylabel('sin') return ax1, ax2 # Create some mock data t = np.arange(0.01, 10.0, 0.01) s1 = np.exp(t) s2 = np.sin(2 * np.pi * t) # Create axes fig, (ax1, ax2) = plt.subplots(1,2, figsize=(10,4)) ax1, ax1a = two_scales(ax1, t, s1, s2, 'r', 'b') ax2, ax2a = two_scales(ax2, t, s1, s2, 'gold', 'limegreen') # Change color of each axis def color_y_axis(ax, color): """Color your axes.""" for t in ax.get_yticklabels(): t.set_color(color) color_y_axis(ax1, 'r') color_y_axis(ax1a, 'b') color_y_axis(ax2, 'gold') color_y_axis(ax2a, 'limegreen') plt.tight_layout() plt.show() 字段并:

categories_ids integer[]

答案 1 :(得分:0)

您无法在阵列列上创建FK。不支持。 因此要么使用EAV模型(https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model,所以3个表而不是2个),或者没有外键(可以创建自定义触发器来模拟它们)。

有些文章将EAV与使用“非原始”(int[]hstorejsonb)数据类型进行比较 - 例如http://coussej.github.io/2016/01/14/Replacing-EAV-with-JSONB-in-PostgreSQL/和{{3有一些基准。