Query tool response pgAdmin - postgresql

ALTER TABLE ONLY rental ADD CONSTRAINT rental_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer(customer_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ERROR: there is no unique constraint matching given keys for referenced table "customer"
ALTER TABLE ONLY customer ADD CONSTRAINT customer_store_id_fkey FOREIGN KEY (store_id) REFERENCES store(store_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY film ADD CONSTRAINT film_language_id_fkey FOREIGN KEY (language_id) REFERENCES language(language_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY inventory ADD CONSTRAINT inventory_store_id_fkey FOREIGN KEY (store_id) REFERENCES store(store_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ERROR: insert or update on table "inventory" violates foreign key constraint
I get these messages when i use the query tool for a sql query of over 1300 lines of code. I'm still learning so any pointers will be helpful

Related

use one part of composite primary key as foreign key

I'm using PostgreSQL.
I have a table accounts with account_id as the primary key. I also have a second table called relations with a composite primary key (follower_id, following_id). Each relation must be unique.
ALTER TABLE accounts ADD CONSTRAINT users_pk PRIMARY KEY (account_id);
ALTER TABLE relations ADD CONSTRAINT relations_pk PRIMARY KEY (follower_id, following_id);
I want to create a foreign key constraint from follower_id (relations) -> account_id (accounts), and the same with following_id.
ALTER TABLE relations ADD CONSTRAINT follower_id_fk FOREIGN KEY (follower_id) REFERENCES accounts (account_id) ON DELETE CASCADE
This foreign key is not accepted by the database. I get the following error:
ERROR: insert or update on table "relations" violates foreign key constraint "follower_id_fk"
DETAIL: Key (follower_id)=(4) is not present in table "accounts".
I understand this, because it's a composite primary key.
What I want to achieve:
When an account is deleted, I want to delete all the records where the account_id is the follower_id (ON DELETE CASCADE) AND where it is the following_id.
I could do this in my nodejs code or with a trigger function, but I don't know what will be the best performance-wise. Does anyone knows a/the best solution?

Foreign key issue while insert or delete

i have 3 tables below
books
users
and
test.users_books with foreign keys
CREATE TABLE test.users_books (
user_id int NOT NULL,
PRIMARY KEY (user_id),
FOREIGN KEY (user_id) REFERENCES test.users(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES test.books(id) ON UPDATE CASCADE ON DELETE CASCADE
);
while am trying to insert in to
INSERT INTO test.users_books values(11)
getting below error
ERROR: insert or update on table "users_books" violates foreign key constraint "users_books_user_id_fkey"
DETAIL: Key (user_id)=(11) is not present in table "users".
********** Error **********
ERROR: insert or update on table "users_books" violates foreign key constraint "users_books_user_id_fkey"
SQL state: 23503
Detail: Key (user_id)=(11) is not present in table "users".
how to insert 11 into this table with foreign key the id 11 having in books table.
is there any condition need insert for this table please let meknow

Why can't I drop this FOREIGN KEY Constraint for my accounts table?

I have an accounts table that has this FOREIGN KEY constraint on it:
TABLE "edits" CONSTRAINT "edits_account_id_fkey1" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
I want to drop this constraint but everytime I try to execute this command below:
ALTER TABLE accounts DROP CONSTRAINT edits_account_id_fkey1;
I get this error:
ERROR: constraint "edits_account_id_fkey1" of relation "accounts" does not exist
It clearly exists. I am looking at it with the \d accounts command. Why is this happening?
-------------EDIT-----------
accounts
Indexes:
........
Check constraint:
......
Foreign-key constraints:
"accounts_about_markup_id_fkey" FOREIGN KEY (about_markup_id) REFERENCES markups(id) ON DELETE CASCADE
"accounts_best_vita_id_fkey" FOREIGN KEY (best_vita_id) REFERENCES vitae(id)
"accounts_organization_id_fkey" FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE
Referenced by:
TABLE "account_reports" CONSTRAINT "account_reports_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
TABLE "actions" CONSTRAINT "actions_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
TABLE "api_keys" CONSTRAINT "api_keys_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
TABLE "authorizations" CONSTRAINT "authorizations_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
TABLE "positions" CONSTRAINT "claims_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
TABLE "duplicates" CONSTRAINT "duplicates_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
TABLE "old_edits" CONSTRAINT "edits_account_id_fkey" FOREIGN KEY (account_id) REFERENCES accounts(id)
TABLE "edits" CONSTRAINT "edits_account_id_fkey1" FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE CASCADE
etc...... etc......
The constraint is placed on the table edits, but you're altering the table accounts. Change accounts in the query to edits and then it will work.

Postgres table update taking 40 minutes, ultimately failing

I'm trying to update the value of a column in all rows in a table with about 1 million records.
First few times I ran the query it hung and after 20 minutes I cancelled the query. I ran it again with EXPLAIN ANALYZE, and after 40 minutes this was output:
=# explain analyze update documents set state = 'archived';
NOTICE: word is too long to be indexed
DETAIL: Words longer than 2047 characters are ignored.
ERROR: deadlock detected
DETAIL: Process 17080 waits for ShareLock on transaction 14275765; blocked by process 1530.
Process 1530 waits for ShareLock on transaction 14273749; blocked by process 17080.
HINT: See server log for query details.
Time: 2324900.382 ms
Here's the EXPLAIN output:
=# explain update documents set workflow_state = 'archived';
QUERY PLAN
----------------------------------------------------------------------------
Update on documents (cost=0.00..220673.50 rows=900750 width=1586)
-> Seq Scan on documents (cost=0.00..220673.50 rows=900750 width=1586)
Any idea what's going on?
Details:
PG version 9.3.7
Indexes:
"documents_pkey" PRIMARY KEY, btree (id)
"document_search_ix" gin (contents_search)
"document_user_id_recvd_ix" btree (user_id, bill_date DESC)
Foreign-key constraints:
"documents_biller_id_fkey" FOREIGN KEY (biller_id) REFERENCES billers(id) ON DELETE SET DEFAULT
"documents_billercred_id_fkey" FOREIGN KEY (billercred_id) REFERENCES billercreds(id) ON DELETE SET NULL
"documents_folder_id_fkey" FOREIGN KEY (folder_id) REFERENCES folders(id) ON DELETE CASCADE
"documents_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
"documents_vendor_id_fkey" FOREIGN KEY (vendor_id) REFERENCES vendors(id) ON DELETE SET NULL
Referenced by:
TABLE "document_billcom_actions" CONSTRAINT "document_billcom_actions_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "document_box_actions" CONSTRAINT "document_box_actions_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "document_email_forwarding_actions" CONSTRAINT "document_email_forwarding_actions_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "document_qbo_actions" CONSTRAINT "document_qbo_actions_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "document_xero_actions" CONSTRAINT "document_xero_actions_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "document_xerofiles_actions" CONSTRAINT "document_xerofiles_actions_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "documenttagmap" CONSTRAINT "documenttagmap_document_id_fkey" FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE
TABLE "synced_docs" CONSTRAINT "synced_docs_doc_id_fkey" FOREIGN KEY (doc_id) REFERENCES documents(id) ON DELETE CASCADE
Triggers:
document_search_update BEFORE INSERT OR UPDATE ON documents FOR EACH ROW EXECUTE PROCEDURE tsvector_update_trigger('contents_search', 'pg_catalog.english', 'contents', 'filename', 'account_name', 'account_number')
document_updated_at_t BEFORE UPDATE ON documents FOR EACH ROW EXECUTE PROCEDURE update_updated_at_column()
documents_count BEFORE INSERT OR DELETE ON documents FOR EACH ROW EXECUTE PROCEDURE count_trig()
folder_document_count_trig BEFORE INSERT OR DELETE OR UPDATE ON documents FOR EACH ROW EXECUTE PROCEDURE update_folder_count()
tags_in_trash_document_count_trig BEFORE DELETE OR UPDATE ON documents FOR EACH ROW EXECUTE PROCEDURE update_tag_trash_count()

TSQL alter table adding constraint for both cascade on delete and update

I'm trying to create a constraint with both on update and delete in tsql. I've tried a couple of different methods, and now I'm a little stuck & frustrated - seems so simple. I know you can't alter an existing constraint so I'm not sure about how to do this;
alter table AllowedCars
add constraint FK_AllowedCars_CarID foreign key (CarID)
references Cars(LocusID) on delete cascade,
constraint FK_AllowedCars_CarID foreign key (CarID)
references Cars(CarID) on update cascade
or this;
alter table AllowedCars add constraint FK_AllowedCars_CarID foreign key (CarID)
references Cars(CarID) on delete cascade and on update cascade
You need to drop constraint first, and then recreate it. Your second attempt was right, but you needed to remove and.
alter table AllowedCars
drop constraint FK_AllowedCars_CarID
alter table AllowedCars
add constraint FK_AllowedCars_CarID
foreign key (CarID)
references Cars(CarID)
on delete cascade
on update cascade