I'm inserting a value in table A, that has a serial type as primary key. I wanna use the returned value of the query as a foreign key of table B... but I get this message:
ERROR: insert or update on table "tb_midia_pessoa" violates foreign key constraint "tb_midia_pessoa_id_pessoa_fkey" DETAIL: Key (id_pessoa)=(30) is not present in table "tb_pessoa". )
How can I make this possible without:
starting a new Transaction
droping my foreign keys constraints =O
?
Regards!
Pedro
You can make a deferrable FK, just use DEFERRABLE and maybe INITIALLY DEFERRED, that's up to you.
http://www.postgresql.org/docs/current/static/sql-createtable.html
The statement below allows non-deferrable constraints to be deferred until transaction committed. If you don't want to change FK definitions.
SET CONSTRAINTS ALL DEFERRED;
Related
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?
I have a table with a foreign key reference and I had added a on_delete_cascade condition with that foreign key.
I don't need the rows to be deleted even if the foreign key object gets deleted.
How can I change the drop condition without have to drop the column?
Just drop the conatraint and then add it back without the ON DELETE CASCADE clause:
ALTER TABLE some_table DROP CONSTRAINT some_key,
ADD CONSTRAINT some_key FOREIGN KEY (id) REFERENCES tab(a_id);
Check what the real experts wrote by reading here:
https://www.postgresql.org/message-id/CABvLTWHdT0tTygV0-O_ZgLRRAGZAg0W4zvghfF2PshAzvkAaGg%40mail.gmail.com
When dropping constraints from a postgres table , How to know the safest order to drop the constraints. Like ,
1) The foreign key constraints can be dropped first [as they have to be droppped before primary key constraint]
Then the order of removing the constraints can be in any order . like check constraint, unique constraint, not nulls, default , primary key constraints . Am I correct
No constraint on a PostgreSQL table depends on another constraint on the same table, so the order does not matter here.
The only dependency between constraints is the dependency of a foreign key on the primary or unique key on the target table.
So you can either remove all foreign key constraints first and then all other constraints, or you can use ALTER TABLE ... DROP CONSTRAINT ... CASCADE which will automatically drop all dependent constraints, then you don't have to care about the order at all.
I am trying to create a foreign key in a new table and that key from the source table already has pre-existing constraint so do i have to redo the constraint on the new table or it will automatically follow that constraint condition?
If I have to redo what would a typical SQL statement be?
There is no need to do redo the constraint while referencing a foreign key which already has a constraint
I'm building my first PostgreSQL database. Now it's just two tables (table A with 1:N relation to table B). Table A has a non-deferrable primary key, while table B has a deferrable primary key. When I try to add table C with a foreign key pointing to primary key of table B (another 1:N relation), PgAdmin refuses to create the table and it returns an error message:
ERROR: cannot use a deferrable unique constraint for referenced table
"table_B"
From my previous searches deferrable primary key seemed to be normal solution. Is it really possible in PgAdmin? Do I need some special trick for this to run?
I use PostgreSQL 9.1.3 and PgAdmin III 1.14.3 on 32 bit Windows XP.
I believe it's not possible. Quote from PostgreSQL docs about foreign key:
The referenced columns must be the columns of a non-deferrable unique
or primary key constraint in the referenced table.