Postgres Update Query - postgresql

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

Related

Row not getting updated after PostgresSql query

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

CREATE Statement in PostgreSQL error relation does not exist

My question is similar to the one posted earlier in the Community. questions/62936399/error-sql-state-42703-while-trying-to-insert-data-into-my-table
In PostgreSQL, I'm trying to run CREATE Table Statement so that I can continue with inserting values. Although my CREATE statement fails so I can't get on with INSERT statement. The error message that keeps coming up ERROR: relation "eventrequest" does not exist
SQL state: 42P01
I have re-did the entire CREATE Statement twice although the error message does not change.
CREATE TABLE CUSTOMER
(CustNo VARCHAR(8) CONSTRAINT CustNoNotNull NOT NULL,
CustName VARCHAR(30) CONSTRAINT CustNameNotNull NOT NULL,
Address VARCHAR(50) CONSTRAINT AddressNotNull NOT NULL,
Internal CHAR(1) CONSTRAINT InternalNotNull NOT NULL,
Contact VARCHAR(35) CONSTRAINT ContractNotNull NOT NULL,
Phone VARCHAR(11) CONSTRAINT CPhoneNotNull NOT NULL,
City VARCHAR(30) CONSTRAINT CityNotNull NOT NULL,
State VARCHAR(2) CONSTRAINT StateNotNull NOT NULL,
Zip VARCHAR(10) CONSTRAINT ZipNotNull NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CustNo)
);
CREATE TABLE FACILITY
(FacNo VARCHAR(8) CONSTRAINT FacNoNotNull NOT NULL,
FacName VARCHAR(30) CONSTRAINT FacNameNotNull NOT NULL,
CONSTRAINT PK_FACILITY PRIMARY KEY (FacNo),
CONSTRAINT Unique_FacName UNIQUE(FacName)
);
CREATE TABLE LOCATION
(LocNo VARCHAR(8) CONSTRAINT LocNoNotNull NOT NULL,
FacNo VARCHAR(8),
LocName VARCHAR(30) CONSTRAINT LocNameNotNull NOT NULL,
CONSTRAINT PK_LOCATION PRIMARY KEY (LocNo),
CONSTRAINT FK_FACNO FOREIGN KEY (FacNo) REFERENCES FACILITY (FacNo)
);
CREATE TABLE EMPLOYEE
(
EmpNo CHAR(11) CONSTRAINT EmpNoNotNull NOT NULL,
EmpName VARCHAR(30) CONSTRAINT EmpNameNotNull NOT NULL,
Department VARCHAR(30) CONSTRAINT DepartmentNotNull NOT NULL,
Email VARCHAR(255) CONSTRAINT EmailNotNull NOT NULL,
Phone VARCHAR(30) CONSTRAINT PhoneNotNull NOT NULL,
CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EmpNo)
);
CREATE TABLE EVENTPLAN
(
PlanNo VARCHAR(8) NOT NULL,
EventNo VARCHAR(8) NOT NULL,
workdate DATE NOT NULL,
notes VARCHAR(40),
activity VARCHAR(20) NOT NULL,
empno VARCHAR(8),
CONSTRAINT PK_PLANNO PRIMARY KEY (PlanNo),
CONSTRAINT FK_EVENTNO FOREIGN KEY (EventNo) REFERENCES EventRequest (EventNo)
);
CREATE TABLE EVENTREQUEST
(
EventNo VARCHAR(8) NOT NULL,
DateHeld DATE NOT NULL,
DateReq DATE NOT NULL,
FacNo VARCHAR(8) NOT NULL,
CustNo VARCHAR(8) NOT NULL,
DateAuth DATE,
Status VARCHAR(8) NOT NULL CHECK (Status IN ('Pending', 'Denied', 'Approved')),
EstCost DECIMAL(10, 2) NOT NULL,
EstAudience INT NOT NULL CHECK (EstAudience > 0),
BudNo VARCHAR(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY (EventNo),
CONSTRAINT FK_FACILITYNOEVENTREQ FOREIGN KEY (FacNo) REFERENCES Facility (FacNo),
CONSTRAINT FK_CUSTOMERNO FOREIGN KEY (CustNo) REFERENCES Customer (CustNo)
);
CREATE TABLE EVENTPLANLINE
(
PlanNo CHAR(8) NOT NULL,
LineNo CHAR(8) NOT NULL,
LocNo CHAR(8) NOT NULL,
ResNo CHAR(8) NOT NULL,
TimeStart TIMESTAMP NOT NULL,
TimeEnd TIMESTAMP NOT NULL,
NumberFLD INTEGER NOT NULL,
CONSTRAINT PK_EVENTPLANLINE PRIMARY KEY (PlanNo, LineNo),
CONSTRAINT FK_EVENTPLAN FOREIGN KEY (PlanNo) REFERENCES EventPlan (PlanNo),
CONSTRAINT FK_LOCATION FOREIGN KEY (LocNo) REFERENCES Location (LocNo),
CONSTRAINT FK_RESOURCETBL FOREIGN KEY (ResNo) REFERENCES ResourceTbl (ResNo)
);
CREATE TABLE RESOURCETBL
(
ResNo CHAR(8) NOT NULL,
ResName VARCHAR(30) NOT NULL,
Rate DECIMAL(8, 2) NOT NULL,
CONSTRAINT PK_RESOURCETBL PRIMARY KEY (ResNo)
);

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

with migration to fill slug with default value

In my Laravel 5.6/PostgreSQL 10.5 application
I have 2 tables :
CREATE TABLE public.rt_genres (
id serial NOT NULL,
published bool NULL DEFAULT false,
created_at timestamp NOT NULL DEFAULT now(),
updated_at timestamp NULL,
CONSTRAINT rt_genres_pkey PRIMARY KEY (id)
)...
CREATE TABLE public.rt_genre_translations (
id serial NOT NULL,
genre_id int4 NOT NULL,
"name" varchar(100) NOT NULL,
description text NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
locale varchar(2) NOT NULL,
CONSTRAINT genre_translations_genre_id_locale_unique UNIQUE (genre_id, locale),
CONSTRAINT rt_genre_translations_pkey PRIMARY KEY (id),
CONSTRAINT genre_translations_genre_id_foreign FOREIGN KEY (genre_id) REFERENCES rt_genres(id) ON DELETE CASCADE
)
I need to add slug field in first rt_genres table
with migration rule:
$table->string('slug', 105)->unique();
and got error :
: Unique violation: 7 ERROR: could not create unique index "genres_slug_unique"
DETAIL: Key (slug)=(id) is duplicated.")
1)If there is a way to assign in migration some unique default value, like = id
->default('rt_genres.id')
?
2) That would be cool to assign to slug value from public.rt_genre_translations.name as I use
"cviebrock/eloquent-sluggable": "^4.5" plugin in my app ? Can I do it ?
Thank you!
You can only get the default value from a different column with a trigger (SO answer).
You can make the column nullable:
$table->string('slug', 105)->nullable()->unique();
Or you create the column, insert unique values and then create the index:
Schema::table('rt_genres', function($table) {
$table->string('slug', 105);
});
DB::table('rt_genres')->update(['slug' => DB::raw('"id"')]);
Schema::table('rt_genres', function($table) {
$table->unique('slug');
});

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.