I have a table B that has a foreign key to table A, and now I want to do a sorta "DELETE CASCADE" thingie on A, but PostgreSQL won't accept the following:
DELETE FROM ATable WHERE aid IN
(
DELETE FROM BTable WHERE aid IN
(
... [expression that depends on the entries in BTAble] ...
)
RETURNING aid
);
Seems that only SELECT can be inside the IN ()
clause. I suppose there is some easy (and standard SQL, not PostgreSQL-specific?) way of doing this?
Edit: is it safe to say that something is badly structured when you bump into this kind of problem? In our case I have a gut feeling that the hits in ..[expr]..
should be in a new CTAble instead of as a subset in ATable, but I can't really point to any design paradigm to support that.
DB functions are outside my comfort zone (I know, I know) and I didn't want to make even temporary changes to the columns in question so I simply did a
CREATE TABLE CTable AS ... [expression that depends on BTAble] ...;
and used that to sequentially delete data in B and A.