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
Related
In a previous command, I foolishly wrote:
alter table UserInfo
add column gcal_id integer references GoogleCal on delete cascade
I've since realized that I don't want on delete cascade. How do I alter gcal-id in UserInfo to no longer have that constraint without losing the information saved in current entries?
Happily, it's fairly simple.
First \d+ UserInfo to see the constraint name, which will appear below the table's column definitions.
In your case it will probably be something like
Foreign-key constraints:
"userinfo_gcal_id_fkey" FOREIGN KEY (gcal_id) REFERENCES googlecal(id) ON DELETE CASCADE
Then, just drop and re-add the constraint in one command:
ALTER TABLE UserInfo
DROP CONSTRAINT userinfo_gcal_id_fkey,
ADD CONSTRAINT userinfo_gcal_id_fkey FOREIGN KEY (gcal_id) REFERENCES googlecal(id);
omitting the ON DELETE CASCADE part.
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.
In order to delete some rows referenced by a foreign key constraint without cascading on delete, I created a temporary foreign key constraint, deleted the row, and then deleted the temporary constraint:
ALTER TABLE rel_user_right
ADD CONSTRAINT temp_fk_rel_user_right_user_right_02
FOREIGN KEY (right_id) REFERENCES user_right (id)
ON DELETE CASCADE;
DELETE FROM user_right WHERE "name" LIKE '%.statusLight.%';
ALTER TABLE rel_user_right
DROP CONSTRAINT temp_fk_rel_user_right_user_right_02;
where this table already had the following constraint defined on it:
ALTER TABLE rel_user_right
ADD CONSTRAINT fk_rel_user_right_user_right_02
FOREIGN KEY (right_id) REFERENCES user_right (id);
This worked fine for me, but seems to have failed on my colleague's computer. As you can see, the two FK constraints define conflicting ON DELETE behaviour. Is precedence defined in this situation, or is it non-deterministic?
Postgres allows to create two references differing only in ON DELETE clause.
I could find no information on the impact of such a case.
In my tests I was unable to cover the existing constraint with new one (i.e. DELETE was always restricted despite of the existence of the second cascading constraint).
However this behaviour is undocumented and one should not rely on it.
The normal way to proceed should be replacing the old constraint with new one:
ALTER TABLE rel_user_right
ADD CONSTRAINT fk_rel_user_right_user_right_temp
FOREIGN KEY (right_id) REFERENCES user_right (id)
ON DELETE CASCADE,
DROP CONSTRAINT fk_rel_user_right_user_right;
DELETE FROM user_right WHERE "name" LIKE '%.statusLight.%';
ALTER TABLE rel_user_right
ADD CONSTRAINT fk_rel_user_right_user_right
FOREIGN KEY (right_id) REFERENCES user_right (id),
DROP CONSTRAINT fk_rel_user_right_user_right_temp;
DISABLE CONSTRAINT would be useful here, but there is no such feature in Postgres (there have been attempts to implement it, but they did not end in success). You can use DISABLE TRIGGER for it, but the above solution is simpler and more natural.
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;