Postgres错误"错误:INSERT具有比表达式更多的目标列"

时间:2016-12-05 21:56:32

标签: postgresql plpgsql

我有以下表格:

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

以下TRIGGER FUNCTION:

-- 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

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的目标列多于表达式LINE 2:   ... user,row_updated_by_db_user,created_by_client,updated_by ...                                                                ^ QUERY:插入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。*语境:PL / pgSQL函数participant_audit()在SQL语句的第3行

     

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

     

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

如何解决此问题?

1 个答案:

答案 0 :(得分: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中包含的要多得多,因为你的insert语句中只有10列。我相信你的一些列可能会采用NULL值。在触发器内语句的SELECT部分​​显式传递空值。