Foreign key issue while insert or delete - postgresql

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

Related

Query tool response pgAdmin

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

What's the right way to use "on cascade delete" on a join table? [duplicate]

I have users, have offers, and a junction table users_offers.
Is there a setup I can carry out with foreign key relations that can ensure that junction data will be automatically deleted when I delete any users or offers?
Declare a referential action: ON DELETE CASCADE, for example:
CREATE TABLE user (
user_id int PRIMARY KEY
);
CREATE TABLE offer (
offer_id int PRIMARY KEY
);
CREATE TABLE user_offer (
user_id int,
offer_id int,
PRIMARY KEY (user_id, offer_id),
FOREIGN KEY (user_id) REFERENCES user (user_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES offer (offer_id) ON DELETE CASCADE
);
[SQL Fiddle]
Interestingly enough, it seems that specifying referential action in "shorthand" foreign key syntax doesn't work (confirmed under MySQL 5.5.30, 5.6.6 m9). The following gets parsed, but when user is deleted, the corresponding user_offer doesn't get deleted:
CREATE TABLE user_offer (
user_id int REFERENCES user (user_id) ON DELETE CASCADE,
offer_id int REFERENCES offer (offer_id) ON DELETE CASCADE,
PRIMARY KEY (user_id, offer_id)
);
You can specify this within your model creation:
CREATE TABLE users_offers (user_id INT NOT NULL,
offer_id INT NOT NULL,
PRIMARY KEY (user_id, offer_id),
FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE,
FOREIGN KEY (offer_id) REFERENCES offers(id)
ON DELETE CASCADE);
You can see a working example in this Fiddle.

PostgreSQL Error: insert or update on table violates foreign key constraint

I have a foreign key constraint on a table and when I’m inserting data I get the following error:
ERROR: insert or update on table "gl_account_item" violates foreign key constraint "fk_gl_account_id" DETAIL: Key (gl_account_id)=(939) is not present in table "gl_account". SQL state: 23503
…if I query the table I can clearly see that it is:
Here is the CREATE TABLE statement:
I cannot understand why I'm getting a foreign key constraint violation error when the id is clearly there in the primary key table. If I remove the foreign key constraint and insert the data and put the constraint back and run a query with a join on that field everything works and the data is there:
Please help.

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