Postgres创建表错误

时间:2017-11-06 18:53:22

标签: sql postgresql

我正在尝试在postgres中创建我的第一个表,但是当我执行此SQL时:

create table public.automated_group_msg (
  automated_group_msg_idx integer NOT NULL DEFAULT nextval ('automated_group_msg_idx'::regclass),
  group_idx integer NOT NULL,
  template_idx integer NOT NULL,
  CONSTRAINT automated_group_msg_pkey PRIMARY KEY (automated_group_msg_idx),
  CONSTRAINT automated_group_msg_group_idx_fkey FOREIGN KEY (group_idx)
  REFERENCES public.groups (group_idx) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT automated_msg_template_idx_fkey FOREIGN KEY (template_idx)
  REFERENCES public.template (template_idx) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE CASCADE
)
WITH (
OIDS = FALSE
);

我收到以下错误:

  

错误:关系" automated_group_msg_idx"不存在

1 个答案:

答案 0 :(得分:1)

您的错误(可能)是因为您尝试使用的序列尚未存在。

但您可以使用以下语法动态创建序列:

create table public.automated_group_msg (
  id serial primary key,
  ... -- other columns
)

与您的问题没有直接关系,但是在列名称中使用表名称命名列通常是反模式,特别是对于id是行业标准的主键。它还允许使用id列始终为id的抽象类进行应用程序代码重构。它清楚地表明automated_group_msg.id的意思,也很清楚automated_group_msg.automated_group_msg_id是一列火车残骸并且包含多余的信息。由于同样的原因,customer.birth_date等属性列名称也不应过度修饰为customer.customer_birth_date