Row not getting updated after PostgresSql query - postgresql

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

Related

Postgres Update Query

I'm executing below query in postgres 12.11
Table structure as below:
CREATE TABLE pdb_interface.schedule (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
report varchar NOT NULL,
users varchar NOT NULL,
"hour" int4 NULL,
min int4 NULL,
daily_frequency int4 NULL,
daily_workday bool NULL,
weekly_frequency int4 NULL,
weekly_days varchar NULL,
monthly_sected_days varchar NULL,
last_updated timestamp NULL,
deleted bool NULL,
CONSTRAINT schedule_pkey PRIMARY KEY (id),
CONSTRAINT schedule_report_fkey FOREIGN KEY (report) REFERENCES pdb_interface.report("Id"),
CONSTRAINT schedule_users_fkey FOREIGN KEY (users) REFERENCES pdb_interface.users("Id"));
update schedule
set daily_frequency = 12
where id = 'f2f0ba60-f8b1-49ae-a82c-aaddb1a1834b'
The column "id" is of uuid and value is present in the table . However while executing above query, it is giving below error
SQL Error [XX000]: ERROR: no conversion function from character varying to uuid
Is there any work around for the same

Why can't I insert a null value into a notNull column which has a default value? [duplicate]

I just migrated my app from mysql to postgres but when I try to insert a record in a specific table I get violates not-null constraint error:
ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).
********** Error **********
ERROR: null value in column "id" violates not-null constraint
SQL state: 23502
Detail: Failing row contains (null, 1, 1, null, null, null, 2016-03-09 09:24:12.841891, 2012-12-31 23:00:00, 2012-12-31 23:00:00, null, null, f, null, f, XYZAssignment, null, null, null, null).
When I try to create the record using factory_girl:
#assignment = FactoryGirl.create(:assignment)
It builds this sql query:
INSERT INTO assignments(
id, account_id, l_id, viewed_at, accepted_at, declined_at,
expires_at, created_at, updated_at, decline_reason, decline_reason_text,
promotion, c_checked_at, forwardable, type, f_promo,
c_check_successful, c_check_api_result, c_check_human_result)
VALUES (null, 1, 1, null, null, null, '2016-03-09 09:24:12.841891', '2012-12-31 23:00:00', '2012-12-31 23:00:00', null, null, 'f', null, 'f', 'XYZAssignment', null, null, null, null);
This is the assignment factory:
FactoryGirl.define do
factory :assignment do
expires_at 24.hours.from_now
account
lead
end
end
this is the table description:
CREATE TABLE assignments(
id serial NOT NULL, account_id integer NOT NULL, l_id integer NOT NULL, viewed_at timestamp without time zone, accepted_at timestamp without time zone, declined_at timestamp without time zone, expires_at timestamp without time zone, created_at timestamp without time zone, updated_at timestamp without time zone, decline_reason character varying(16), decline_reason_text character varying(256), promotion boolean NOT NULL DEFAULT false, c_checked_at timestamp without time zone, forwardable boolean DEFAULT true, type character varying(64), f_promo boolean, c_check_successful boolean, c_check_api_result character varying(32), c_check_human_result character varying(32), CONSTRAINT assignments_pkey PRIMARY KEY (id)
) WITH (
OIDS=FALSE
);
Looks its not able to auto increment the id, any idea?
You have to skip id in the INSERT operation:
INSERT INTO assignments(account_id, l_id, ...)
VALUES
(1, 1, ...)
The id will automatically get the next sequence number, since it is an auto-increment field.

PostgreSQL trigger for inserting an entry in table2 AFTER a row is inserted in table1 (using the values from newly inserted row in table1)

I have displayed my database schema below.
CREATE TABLE table_group
(
group_id serial NOT NULL,
group_creator_id integer NOT NULL,
group_name character varying(20) NOT NULL,
active_status boolean NOT NULL,
PRIMARY KEY (group_id),
)
CREATE TABLE table_group_members
(
group_member_id serial NOT NULL,
group_id integer REFERENCES table_group (group_id) NOT NULL,
member_user_id integer REFERENCES table_group (group_creator_id) NOT NULL,
PRIMARY KEY (group_member_id)
)
I want to INSERT a new row in the table table_group_members as soon a new row is inserted in table table_group.
I want to pass the group_id and group_creator_id of the newly inserted row in the table table_group as parameters to INSERT query for the table table_group_members
How should I do it using the PostgreSQL TRIGGER?

Flyway: Relation does not exist

ERROR: relation "signature_level" does not exist
I'm having trouble figuring out what's the problem. Flyway is throwing me this error when migrating.
CREATE TABLE IF NOT EXISTS "user" (
id SERIAL NOT NULL PRIMARY KEY,
name text NOT NULL,
id_code numeric NOT NULL,
email text NOT NULL,
address text,
alt_contact_relation text NULL,
alt_contact_phone numeric NULL,
signature_level_id integer NULL,
username text NOT NULL,
password text NOT NULL,
create_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
update_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
status active_status NOT NULL DEFAULT 'active',
work_detail_id integer NULL,
CONSTRAINT FK_user_signature_level FOREIGN KEY (signature_level_id) REFERENCES signature_level (id)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT FK_user_work_detail FOREIGN KEY (work_detail_id) REFERENCES work_detail (id)
ON DELETE NO ACTION ON UPDATE NO ACTION
);
This is the signature level table.
CREATE TABLE IF NOT EXISTS "signature_level" (
id SERIAL NOT NULL PRIMARY KEY,
name text NOT NULL,
create_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL,
update_time TIMESTAMP without TIME ZONE DEFAULT now() NOT NULL
);

postgresql cannot insert data to newly added column

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.