尝试在heroku中设置postgres表,但在尝试在表的主键中设置自动递增时出现此错误
user_id_seq relation does not exist
这是create语句

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" ) );

这个错误是什么意思?我必须事先建立这种关系吗?

最佳答案

你必须先做些类似的事情:

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

或者声明您的列idcreate the equence
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;

10-07 22:33