I want to delete rows from two tables, which have one reliance on each other through anti-procrastination obstacles. To make this post easier, I joked a simple DB schema.
I'm hoping to remove entries from some table, inside a SQL transaction / DB patch, 'delete_from_me' is the catch, I want to delete a second table based on the 'constraining_table' selection Before I remove the link myself.
Here are the details of the two tables:
Tab-Quarantine = & gt; \ D delete_from_me table "public.delete_from_me" column | | Type | Modifier ----------- + ------------------- + ----------- Id | Character different | No extensions. Character different | Null not indexed: "delete_from_me_pkey" primary key, BTRI tab-quarantine = & gt; \ D constraining_table table "public.constraining_table" column | Type | Modifier -------- + ------------------- + ----------- Image | Character different | No tap type. Character different | Obstacles to the meaningless foreign: "constraining_table_image_fkey" foreign key (image) reference on the ban order to remove the reference, delete_from_me (id) Here are some sample data that I have been blurred here Is:
tab-quarantine = & gt; SELECT * FROM delete_from_me; ID | Extension ------------ + ----------- 12345abcde | PNG (1 row) tab-quarantine = & gt; Selection * constraining_table; Image | Type ------------ + ---------- 12345abcde | Select_me (1 line) And here's my transaction:
BEGIN; \ Set ON_ERROR_STOP 1 set all configures all defined; Remove from delete_from_me where id (constraining_table WHERE type = 'select_me' from selection image); Remove the place to be removed from the barrier = 'select_me'; COMMIT; This transaction fails when I step through it and manually do this, I get the following error message:
< Code> error: update or delete table "update_from_me" violation of foreign key constraint "constraining_table_image_fkey" table "constraining_table" extension: key (id) = (12345abcde) is still referred to as "constraining_table" table
It looks like a good candidate for a temporary table, though F I want to know do that I should obstacles should not take effect until the end of the transaction can not be removed in this order?
use deleted action on DEFERRABLE instead of DELETE RESTRICTED DEFERRABLE . Instead of using Ristrict , the no action compels the obligation to non-suspend, even if you apply the irreversible modifier Are or Not?
No action can be postponed except for any action check, even if the barrier has been declared deferrable.
Obviously, the above warning contains restriction .
No action taken
/ P>
This indicates that deletion or updating a foreign key generates an error will cause an error. If the barrier has deferred, then this error barrier will be produced at check time if any reference line is present. This is the default action
restriction
This indicates that deletion or updating will cause a foreign key constraint error generated error. This is similar to any action, except that the check is not autocratic.
As you can see, except no action RESTRICT code>, no action Deferrable is the reason why I recommend it - I think this is what you are asking for.
Comments
Post a Comment