【问题标题】:Postgres error "ERROR: INSERT has more target columns than expressions"Postgres 错误“错误:插入的目标列多于表达式”
【发布时间】:2017-04-20 10:13:31
【问题描述】:

我有以下表格:

CREATE TABLE public.participant_audit
(
  participant_audit_id bigint NOT NULL DEFAULT nextval('participant_audit_participant_audit_id_seq'::regclass),
  participant_id bigint,
  shared_asset_id bigint NOT NULL,
  asset_role_type character varying(200) NOT NULL,
  user_external_ref_uuid uuid NOT NULL,
  user_first_name character varying(200) NOT NULL,
  user_last_name character varying(200) NOT NULL,
  user_email_address character varying(200) NOT NULL,
  deleted_timestamp timestamp(0) with time zone,
  row_updated_timestamp timestamp(6) with time zone NOT NULL,
  row_created_timestamp timestamp(6) with time zone NOT NULL,
  row_created_by_db_user oid NOT NULL,
  row_updated_by_db_user oid NOT NULL,
  created_by_client uuid,
  updated_by_client uuid,
  CONSTRAINT participant_audit_pkey PRIMARY KEY (participant_audit_id)
)
WITH (
  OIDS=FALSE
);

CREATE TABLE public.participant
(
  participant_id bigint NOT NULL DEFAULT nextval('participant_participant_id_seq'::regclass),
  shared_asset_id bigint NOT NULL,
  asset_role_type_id bigint NOT NULL,
  user_external_ref_uuid uuid NOT NULL,
  user_first_name character varying(200) NOT NULL,
  user_last_name character varying(200) NOT NULL,
  user_email_address character varying(200) NOT NULL,
  deleted_timestamp timestamp(0) with time zone,
  row_updated_timestamp timestamp(6) with time zone NOT NULL,
  row_created_timestamp timestamp(6) with time zone NOT NULL,
  row_created_by_db_user oid NOT NULL,
  row_updated_by_db_user oid NOT NULL,
  created_by_client uuid,
  updated_by_client uuid,
  CONSTRAINT participant_pkey PRIMARY KEY (participant_id),
  CONSTRAINT participant_asset_role_type_id_fkey FOREIGN KEY (asset_role_type_id)
      REFERENCES public.asset_role_type (asset_role_type_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT participant_shared_asset_id_fkey FOREIGN KEY (shared_asset_id)
      REFERENCES public.shared_asset (shared_asset_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

还有以下触发函数:

-- DROP FUNCTION public.participant_audit();

CREATE OR REPLACE FUNCTION public.participant_audit()
  RETURNS trigger AS
$BODY$
    BEGIN
        insert into participant_audit
                    (participant_audit_id, participant_id , shared_asset_id , asset_role_type , user_external_ref_uuid,
                      user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,
                      row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,
                      updated_by_client
                    )
                   select NEW.* ;
                    RETURN NEW;
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE SECURITY DEFINER
  COST 100;

当我执行以下插入时

INSERT INTO participant (shared_asset_id,asset_role_type_id,
  user_external_ref_uuid,user_first_name,user_last_name,
  user_email_address,row_created_by_db_user,
  row_updated_by_db_user,created_by_client,updated_by_client) 
VALUES (1, 1, 'c9d140ad-b0da-4a9d-a898-8719000c7b7b'::uuid , 'john', 'simpson', 'js@gmail.com', 1::oid,1::oid, '53ed670d-f680-4e81-b53d-59b3d487633f'::uuid, '53ed670d-f680-4e81-b53d-59b3d487633f'::uuid);

我收到以下错误:

错误:INSERT 的目标列多于表达式第 2 行: ...用户,row_updated_by_db_user,created_by_client,updated_by... ^ 查询:插入 public.participant_audit (participant_audit_id、participant_id、shared_asset_id、asset_role_type、 user_external_ref_uuid,user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,updated_by_client) 选择 NEW.* CONTEXT: PL/pgSQL functionparticipant_audit() line 3 at SQL statement

**********错误**********

错误:INSERT 的目标列多于表达式 SQL 状态: 42601 上下文:SQL 中的 PL/pgSQL 函数参与者审核()第 3 行 声明

我该如何解决这个问题??

【问题讨论】:

  • 您没有在 insert 中添加足够的列。从values 之前删除updated_by_client,或者为updated_by_client 添加一个值
  • 我执行了以下操作,仍然出现上述错误:INSERT INTO participant (shared_asset_id,asset_role_type_id,user_external_ref_uuid,user_first_name,user_last_name,user_email_address,row_created_by_db_user ,row_updated_by_db_user) values (1, 1, 'c9d140ad-b0da-4a9d-a898-8719000c7b7b'::uuid , 'john', 'simpson', 'js@gmail.com', 1::oid,1:oid);
  • 我不认为你打算标记这个plsql,因为这个问题与 Oracle 无关。

标签: postgresql plpgsql


【解决方案1】:

问题出在您的触发器上。在此处计算您尝试插入到审计表中的列。

insert into participant_audit
                    (participant_audit_id, participant_id , shared_asset_id , asset_role_type , user_external_ref_uuid,
                      user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,
                      row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,
                      updated_by_client
                    )
                   select NEW.* ;

这比 NEW 中包含的内容要多得多,因为您的插入语句中只有 10 列。我相信您的某些列可能采用 NULL 值。在触发器内的语句的 SELECT 部分中显式传递空值。

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 2015-02-22
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多