尝试在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" ) );
这个错误意味着什么,我必须事先以某种方式建立这种关系吗?
答案 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;