首页 > 解决方案 > Postgresql说任何列都不存在列

问题描述

我使用以下查询创建了一个表

CREATE TABLE public.pg_subscription 
(
    id uuid DEFAULT public.uuid_generate_v4() NOT NULL,
    user_id uuid NOT NULL,
    pg_subscription_plan_id uuid,
    count integer DEFAULT 1 NOT NULL,
    authentication_url text,
    rp_id text,
    status public.pg_subscription_type DEFAULT 'created'::public.pg_subscription_type,
    created_timestamp timestamp with time zone,
    next_billing_date timestamp with time zone
);

我使用此查询使用 PgAdmin 4 创建此表,并且任何插入查询都会导致“列不存在”错误。我在列列表中用作第一列的任何列都会给出相同的错误,因此这不是特定于列的。

无法理解为什么会发生这种情况,任何指针表示赞赏。

编辑 :

CREATE TYPE public.pg_subscription_type AS ENUM (
    'created',
    'authenticated',
    'active',
    'pending',
    'halted',
    'paused',
    'resumed',
    'cancelled'
);

这是失败的查询

insert into pg_subscription(user_id,id,pg_subscription_plan_id,count,authentication_url,rp_id,status,created_timestamp,next_billing_date)
    values ('shjkhfkhf','kdjfkd','dhfhfkh',0,'dfjdfh','kdff',1566656,7878787)

这就是错误

错误:关系“pg_subscription”的列“user_id”不存在第 1 行:插入 pg_subscription(user_id,id,pg_subscription_plan,... ^ SQL 状态:42703 字符:29

此外,该数据库存在于其他服务器上并且工作正常,在通过备份提取模式并在 PgAdmin sql 工具的帮助下恢复后,它出现在新服务器上。谢谢

标签: sqlpostgresql

解决方案


  • 避免关键字作为列名
  • 不要将随机文本分配给 UUID
  • 不要在你的对象名称前加上pg_; 它是为目录保留的

在这里工作:


\i tmp.sql

-- DROP TYPE public.omg_subscription_type ;
CREATE TYPE omg_subscription_type AS ENUM (
    'created'
    , 'authenticated'
    , 'active'
    , 'pending'
    , 'halted'
    , 'paused'
    , 'resumed'
    , 'cancelled'
);

-- DROP TABLE public.omg_subscription ;
CREATE TABLE omg_subscription
(
    -- id uuid DEFAULT public.uuid_generate_v4() NOT NULL
    id uuid DEFAULT public.gen_random_uuid() NOT NULL
    , user_id uuid NOT NULL DEFAULT public.gen_random_uuid() NOT NULL
    , omg_subscription_plan_id uuid DEFAULT public.gen_random_uuid() NOT NULL
    , zcount integer DEFAULT 1 NOT NULL
    , authentication_url text
    , rp_id text
    , status omg_subscription_type DEFAULT 'created'::omg_subscription_type
    , created_timestamp timestamp with time zone
    , next_billing_date timestamp with time zone
);

insert into omg_subscription( zcount, authentication_url, rp_id, status, created_timestamp, next_billing_date)
                        values ( 0, 'dfjdfh', 'wtf', 'created'::omg_subscription_type , '2020-09-04'::timestamp , '2021-09-04'::timestamp )
        ;

SELECT * FROM omg_subscription
        ;

结果:


DROP SCHEMA
CREATE SCHEMA
SET
CREATE TYPE
CREATE TABLE
INSERT 0 1
                  id                  |               user_id                |       omg_subscription_plan_id       | zcount | authentication_url | rp_id | status  |   created_timestamp    |   next_billing_date    
--------------------------------------+--------------------------------------+--------------------------------------+--------+--------------------+-------+---------+------------------------+------------------------
 41bed520-1867-4533-a574-f98db988ab2c | ec05f6d2-754e-4348-a696-24eefe2521c2 | 6ce3bc7d-3a05-47b5-9ab4-a551f582539f |      0 | dfjdfh             | wtf   | created | 2020-09-04 00:00:00+02 | 2021-09-04 00:00:00+02
(1 row)

推荐阅读