Changed Query

0 claps 0 comments Share

DO $$
DECLARE
    r RECORD;
BEGIN
    FOR r IN
        SELECT
            tc.constraint_name,
            tc.table_name,
            kcu.column_name
        FROM information_schema.table_constraints tc
        JOIN information_schema.key_column_usage kcu
            ON tc.constraint_name = kcu.constraint_name
           AND tc.table_schema = kcu.table_schema
        JOIN information_schema.constraint_column_usage ccu
            ON tc.constraint_name = ccu.constraint_name
           AND tc.table_schema = ccu.constraint_schema
        WHERE tc.constraint_type = 'FOREIGN KEY'
          AND tc.table_schema = 'fvalue'
          AND ccu.table_name = 'DocumentSection'
    LOOP
        EXECUTE format(
            'ALTER TABLE fvalue.%I
             DROP CONSTRAINT %I',
            r.table_name,
            r.constraint_name
        );

        EXECUTE format(
            'ALTER TABLE fvalue.%I
             ADD CONSTRAINT %I
             FOREIGN KEY (%I)
             REFERENCES fvalue."DocumentSection"("DocumentSectionId")
             ON DELETE CASCADE',
            r.table_name,
            r.constraint_name,
            r.column_name
        );
    END LOOP;
END $$;

ALTER TABLE fvalue."ContractVersions"
DROP CONSTRAINT "FK_ContractVersions_Contract_ContractId";

ALTER TABLE fvalue."ContractVersions"
ADD CONSTRAINT "FK_ContractVersions_Contract_ContractId"
FOREIGN KEY ("ContractId")
REFERENCES fvalue."Contract"("ContractId")
ON DELETE CASCADE;


ALTER TABLE fvalue."Contract"
DROP CONSTRAINT "FK_Contract_Documents_DocumentId";

ALTER TABLE fvalue."Contract"
ADD CONSTRAINT "FK_Contract_Documents_DocumentId"
FOREIGN KEY ("DocumentId")
REFERENCES fvalue."Documents"("DocumentId")
ON DELETE CASCADE;


ALTER TABLE fvalue."ContractVersions"
DROP CONSTRAINT "FK_ContractVersions_Documents_DocumentId";

ALTER TABLE fvalue."ContractVersions"
ADD CONSTRAINT "FK_ContractVersions_Documents_DocumentId"
FOREIGN KEY ("DocumentId")
REFERENCES fvalue."Documents"("DocumentId")
ON DELETE CASCADE;


ALTER TABLE fvalue."DocumentRelationship"
DROP CONSTRAINT "FK_DocumentRelationship_Documents_DocumentId";

ALTER TABLE fvalue."DocumentRelationship"
ADD CONSTRAINT "FK_DocumentRelationship_Documents_DocumentId"
FOREIGN KEY ("DocumentId")
REFERENCES fvalue."Documents"("DocumentId")
ON DELETE CASCADE;


ALTER TABLE fvalue."DocumentRelationship"
DROP CONSTRAINT "FK_DocumentRelationship_Documents_RelatedDocumentId";

ALTER TABLE fvalue."DocumentRelationship"
ADD CONSTRAINT "FK_DocumentRelationship_Documents_RelatedDocumentId"
FOREIGN KEY ("RelatedDocumentId")
REFERENCES fvalue."Documents"("DocumentId")
ON DELETE CASCADE;

0 claps 0 comments

Written by

atullyalast

Publishing thoughtful stories for readers who want a slower, clearer page.

Responses

0 comments on this story.

Sign in to comment, clap, and join the discussion.

No comments yet

Be the first reader to respond.

More from this publication

Writing resources

Newsletter Best Practices