Select * from fvalue."Documents" where "DocumentUniqueId"='NSD_000444'
Select * from fvalue."Contract" where "DocumentId"=444
Select * from fvalue."ContractVersions" where "ContractId"=347
Select * from fvalue."DocumentRelationship" where "RelatedDocumentId"=444
Select * from fvalue."DocumentSection" where "DocumentSectionId"=936
Select * from fvalue."section_category_and_contact_details" where "DocumentSectionId"=731
Select * from fvalue."section_valuator_company_details" where "DocumentSectionId"=732
select * from fvalue."section_address" where "DocumentSectionId"=733
Select * from fvalue."section_working_area" where "DocumentSectionId"=734
Better Approach
DO $$
DECLARE
v_document_id INT;
v_contract_id INT;
r RECORD;
BEGIN
-- Get DocumentId
SELECT "DocumentId"
INTO v_document_id
FROM fvalue."Documents"
WHERE "DocumentUniqueId" = 'NSD_000554';
IF v_document_id IS NULL THEN
RAISE EXCEPTION 'Document not found.';
END IF;
-- Get ContractId
SELECT "ContractId"
INTO v_contract_id
FROM fvalue."Contract"
WHERE "DocumentId" = v_document_id;
------------------------------------------------------------------
-- Delete every section_* table automatically
------------------------------------------------------------------
FOR r IN
SELECT tc.table_name
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
AND tc.table_schema = 'fvalue'
AND ccu.table_name = 'DocumentSection'
LOOP
EXECUTE format(
'DELETE FROM fvalue.%I
WHERE "DocumentSectionId" IN
(
SELECT "DocumentSectionId"
FROM fvalue."DocumentSection"
WHERE "DocumentId" = $1
)',
r.table_name
) USING v_document_id;
END LOOP;
------------------------------------------------------------------
-- Delete DocumentSection
------------------------------------------------------------------
DELETE FROM fvalue."DocumentSection"
WHERE "DocumentId" = v_document_id;
------------------------------------------------------------------
-- Delete ContractVersions
------------------------------------------------------------------
DELETE FROM fvalue."ContractVersions"
WHERE "ContractId" = v_contract_id
OR "DocumentId" = v_document_id;
------------------------------------------------------------------
-- Delete Contract
------------------------------------------------------------------
DELETE FROM fvalue."Contract"
WHERE "DocumentId" = v_document_id;
------------------------------------------------------------------
-- Delete Relationships
------------------------------------------------------------------
DELETE FROM fvalue."DocumentRelationship"
WHERE "DocumentId" = v_document_id
OR "RelatedDocumentId" = v_document_id;
------------------------------------------------------------------
-- Delete Document
------------------------------------------------------------------
DELETE FROM fvalue."Documents"
WHERE "DocumentId" = v_document_id;
RAISE NOTICE 'Document % deleted successfully.', v_document_id;
END $$;