I understand this can easily be achived with cascade behaviour, but cascade isn't desirable in this case.
Is there a way of automatically either nulling the column or deleting the row referencing a foreign key depending on whether or not the column is nullable in the referencing table?
Say we have the following tables:
Table "public.a"
Column | Type | Modifiers
----------------------------+---------+--------------------
pk | integer | not null
Table "public.b"
Column | Type | Modifiers
----------------------------+---------+--------------------
fk | integer | not null
"b_a_fk_fkey" FOREIGN KEY (fk) REFERENCES a(pk)
Table "public.c"
Column | Type | Modifiers
----------------------------+---------+--------------------
fk | integer |
"c_a_fk_fkey" FOREIGN KEY (fk) REFERENCES a(pk)
We can get tables referencing a table with queries such as this. Does anyone have an example of how to generically delete rows in table 'b' and null columns in table 'c' referencing a given value for a.pk?
Best Answer
EDIT: Recursive version. Lots of debug but peace of mind is nice when you're running queries like this. I'm sure this is by no means perfect, it assumes the PKs are ints for one, but it does the job for me:
Original, more readable but "depth = 1" answer:
In the unlikely event anyone wants to follow in my footsteps here, this is what I ended up with. It only "cascades" one level, but as it happens that's good enough for me for the task in hand. I'll leave recursive cascades to the interested reader ;-)