I have two tables to handle attachments:
- "files" which contains an
idand other info - "attachments" which contains the
id_file(theidin the tablefilesis the foreign key) and other info of the attachments.
Two tables to be able to share a file without duplicating it on the server (for your information).
When I delete an attachment, I want to delete the file only if there is no attachement left referring to its id.
I have this request, to delete all the attachments linked to a particular item (the 26th item of the table TABLE_NAME:
WITH deleted_files AS (
DELETE FROM public.attachments a
WHERE a.id_in_table = 26
AND a.table_name = 'TABLE_NAME'
RETURNING id_file
)
DELETE FROM public.files f
WHERE f.id IN (SELECT id FROM deleted_files)
AND NOT EXISTS (
SELECT 1
FROM public.attachments a
WHERE a.id_file = f.id
);
The problem is:
- the attachments are deleted correctly, but not the files
- if I run the same request again nothing is deleted (this is normal)
- if I run the second
DELETEwith theidsreturned by the first delete, they are correctly deleted.
Is there some mechanism that keeps a shallow copy of the table with the deleted items, thus making the deletion of items impossible?
As the documentation says,
My advice is to run two separate
DELETEstatements. If you need them to share the same snapshot, run them in aREPEATABLE READtransaction. If the reason for using a single statement was a constraint, you can use a deferred constraint instead. Use a temporary table for the rows deleted by the first statement.