在heroku postgres上创建自动增量主键列时出错

时间:2015-05-21 21:19:23

标签: postgresql heroku-postgres

尝试在heroku中设置我的postgres表,并且在尝试在我的表上的主键中设置自动加入时出现此错误

user_id_seq relation does not exist

这是创建声明

CREATE TABLE "public"."user" (
    "id" INTEGER DEFAULT nextval('user_id_seq'::regclass) NOT NULL UNIQUE,
    "uname" CHARACTER VARYING( 255 ) COLLATE "pg_catalog"."default" UNIQUE,
    "description" CHARACTER VARYING( 2044 ) COLLATE "pg_catalog"."default",
    "country" CHARACTER( 3 ) COLLATE "pg_catalog"."default" DEFAULT 'USA'::bpchar NOT NULL,
 PRIMARY KEY ( "id" )
, CONSTRAINT "unique_uname" UNIQUE( "uname" ) );

这个错误意味着什么,我必须事先以某种方式建立这种关系吗?

1 个答案:

答案 0 :(得分:2)

首先必须create the equence使用以下内容:

CREATE SEQUENCE user_id_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;`

或者将列id声明为serial

CREATE TABLE "public"."user" (
    "id" bigserial NOT NULL UNIQUE,
    "uname" CHARACTER VARYING( 255 ) COLLATE "pg_catalog"."default" UNIQUE,
    "description" CHARACTER VARYING( 2044 ) COLLATE "pg_catalog"."default",
    "country" CHARACTER( 3 ) COLLATE "pg_catalog"."default" DEFAULT     'USA'::bpchar NOT NULL,
 PRIMARY KEY ( "id" )
, CONSTRAINT "unique_uname" UNIQUE( "uname" ) );

相当于:

CREATE SEQUENCE user_id_seq;

CREATE TABLE "public"."user" (
    "id" bigint NOT NULL UNIQUE DEFAULT nextval('user_id_seq'),
    "uname" CHARACTER VARYING( 255 ) COLLATE "pg_catalog"."default" UNIQUE,
    "description" CHARACTER VARYING( 2044 ) COLLATE "pg_catalog"."default",
    "country" CHARACTER( 3 ) COLLATE "pg_catalog"."default" DEFAULT     'USA'::bpchar NOT NULL,
 PRIMARY KEY ( "id" )
, CONSTRAINT "unique_uname" UNIQUE( "uname" ) );


ALTER SEQUENCE user_id_seq OWNED BY public.user.id;