I have the following tables :
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
);
And the following 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;
When I execute the following 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);
I get the following error:
ERROR: INSERT has more target columns than expressions LINE 2:
...user , row_updated_by_db_user , created_by_client,updated_by...
^ QUERY: insert into 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)
select NEW.* CONTEXT: PL/pgSQL function participant_audit() line 3 at SQL statement
********** Error **********
ERROR: INSERT has more target columns than expressions SQL state:
42601 Context: PL/pgSQL function participant_audit() line 3 at SQL
statement
How can I fix this issue ??
The problem is in your trigger. Count the columns that you are trying to insert into the audit table here.
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.* ;
That's quite a few more than what's contained in NEW because your insert statement has only 10 columns in it. I believe some of your columns maybe taking NULL values. Pass nulls explicitly in the SELECT part of your statement inside the trigger.
Related
I am trying to create a DB2 table with the following
CREATE TABLE ACQ_FAH_DEV.fah_balance_ledger
(
"ACTIVE" VARCHAR(10),
INPUT_BY VARCHAR(32),
INPUT_TIME DATE,
AMENDED_BY VARCHAR(32),
AMENDED_TIME DATE,
ENTITY VARCHAR(20),
ACCOUNT_CODE VARCHAR(20),
ACCOUNT_NAME VARCHAR(255),
PORTFOLIO_CODE VARCHAR(100),
OM_LOAD_RUN_ID VARCHAR(128) NOT NULL,
OM_LOAD_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_TYP VARCHAR(20) NOT NULL,
BM_BUSINESS_INTERVAL_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_START_END_FLAG VARCHAR(10) NOT NULL,
SM_SOURCE_SYSTEM_CD VARCHAR(16) NOT NULL,
OM_UNIQUE_ROW_ID BIGINT NOT NULL,
OM_USER_ID VARCHAR(100) NOT NULL,
OM_VERSION_ID SMALLINT NOT NULL
)
ORGANIZE BY COLUMN IN ACQ_FAH_DEV
DISTRIBUTE BY HASH(
ACCOUNT_CODE,
OM_VERSION_ID,
BM_BUSINESS_INTERVAL_TYP
);
but running into this error
(SQLSTATE: 42601, SQLCODE: -104): DB2 SQL Error: SQLCODE=-104,
SQLSTATE=42601, SQLERRMC=(;LOAD_RUN_ID VARCHAR;BINARY, DRIVER=4.26.14
SQLSTATE 42601: A character, token, or clause is invalid or missing.
SQL0104N An unexpected token "(" was found following "LOAD_RUN_ID
VARCHAR". Expected tokens may include: "BINARY".
The error seems innocuous but I am not able to see why this is failing.
For LUW, the tablespace clause should be before the organized clause (I assume IN ACQ_FAH_DEV refers to which tablespace to put the table in). Try:
CREATE TABLE ACQ_FAH_DEV.fah_balance_ledger
(
"ACTIVE" VARCHAR(10),
INPUT_BY VARCHAR(32),
INPUT_TIME DATE,
AMENDED_BY VARCHAR(32),
AMENDED_TIME DATE,
ENTITY VARCHAR(20),
ACCOUNT_CODE VARCHAR(20),
ACCOUNT_NAME VARCHAR(255),
PORTFOLIO_CODE VARCHAR(100),
OM_LOAD_RUN_ID VARCHAR(128) NOT NULL,
OM_LOAD_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_TYP VARCHAR(20) NOT NULL,
BM_BUSINESS_INTERVAL_TMST TIMESTAMP(6) NOT NULL,
BM_BUSINESS_INTERVAL_START_END_FLAG VARCHAR(10) NOT NULL,
SM_SOURCE_SYSTEM_CD VARCHAR(16) NOT NULL,
OM_UNIQUE_ROW_ID BIGINT NOT NULL,
OM_USER_ID VARCHAR(100) NOT NULL,
OM_VERSION_ID SMALLINT NOT NULL
)
IN ACQ_FAH_DEV
ORGANIZE BY COLUMN
DISTRIBUTE BY HASH(
ACCOUNT_CODE,
OM_VERSION_ID,
BM_BUSINESS_INTERVAL_TYP
);
Documentation for CREATE TABLE statement
This is my simple query which deletes a row from table A and inserts it into table B with some additional columns. I still see the row in question in table A and not table B. But transaction shows successful with no errors.
with failed as (delete from transactions where transaction_id='12345' returning *)
insert into failed_notifications ("transaction_id") select "transaction_id" from failed;
Response I get from the postgres
Updated Rows 0
Query with failed as (delete from transactions where transaction_id='12345' returning *)
insert into failed_notifications ("transaction_id") select "transaction_id" from failed
Schema
CREATE TABLE public.failed_notifications (
transaction_id text NOT NULL,
"identity" text NULL,
identity_type text NULL,
country_id text NULL,
updated_at int8 NULL,
created_at int8 NULL,
reason text NULL,
CONSTRAINT failed_notifications_pkey PRIMARY KEY (transaction_id)
);
CREATE TABLE public.transactions (
"identity" text NOT NULL,
identity_type text NULL,
service_id text NULL,
partner_id text NULL,
country_id text NULL,
updated_at int8 NULL,
created_at int8 NULL,
transaction_id text NOT NULL,
start_date int8 NULL,
end_date int8 NULL,
is_sms_triggered bool NULL,
CONSTRAINT transactions_pkey PRIMARY KEY (transaction_id)
);
In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.
I want to add the following constraint to the table named service in my database.
Constraint 1:
Neither DateCompleted nor DueDate can precede StartDate.
CREATE TABLE [Service] (
Service_ID INT NOT NULL PRIMARY KEY
, Invoice_ID INT
, Project_ID INT NOT NULL
, Description CHAR(20) NOT NULL
, Start_Date VARCHAR(10) NOT NULL
, Due_Date VARCHAR(10)
, Planned_Price VARCHAR(10)
, Actual_Price VARCHAR(10)
, Status CHAR(10) NOT NULL
, Date_Completed VARCHAR(10)
);
Another constraint should be added to the table named client.
Constraint 2:
Column Post_Code values should be positive integers with three or four digits.
CREATE TABLE [Client] (
Client_ID INT NOT NULL PRIMARY KEY
, First_Name VARCHAR(15) NOT NULL
, Last_Name VARCHAR(15) NOT NULL
, Street_Address VARCHAR(50)
, Suburb VARCHAR(15) NOT NULL
, State VARCHAR(3) NOT NULL
, Post_Code INT NOT NULL
, Phone_number INT NOT NULL
);
Service:
ALTER TABLE [Service]
ADD CONSTRAINT [service_datecheck]
CHECK ([StartDate] <= [DateCompleted] AND [StartDate] <= [DueDate]);
Client:
ALTER TABLE [Client]
ADD CONSTRAINT [client_postcodecheck]
CHECK ([Post_Code] > 0 AND LEN([Post_Code]) IN (3,4));
The below ALTER command is taking long time, but not executing.
alter table DETAILS alter column row_id type numeric(20);
DDL is as follows:
CREATE TABLE Details
(
row_id numeric(15,0) NOT NULL,
intfid character varying(20) NOT NULL,
seqno numeric(15,0) NOT NULL,
record_id numeric(15,0) NOT NULL,
lstmoddate timestamp without time zone NOT NULL,
rcvddate timestamp without time zone NOT NULL DEFAULT current_date,
record_type character varying(60),
xmldata bytea,
CONSTRAINT mrd_pk PRIMARY KEY (rcvddate, intfid, seqno, record_id)
)