-- Add ON DELETE CASCADE to all foreign key constraints referencing candidate_document -- This allows deleting documents without violating foreign key constraints -- Drop and recreate foreign key constraints with ON DELETE CASCADE -- 1. candidate_education.source_document_id ALTER TABLE app.candidate_education DROP CONSTRAINT IF EXISTS candidate_education_source_document_id_fkey; ALTER TABLE app.candidate_education ADD CONSTRAINT candidate_education_source_document_id_fkey FOREIGN KEY (source_document_id) REFERENCES app.candidate_document(document_id) ON DELETE CASCADE; -- 2. candidate_transcript.source_document_id ALTER TABLE app.candidate_transcript DROP CONSTRAINT IF EXISTS candidate_transcript_source_document_id_fkey; ALTER TABLE app.candidate_transcript ADD CONSTRAINT candidate_transcript_source_document_id_fkey FOREIGN KEY (source_document_id) REFERENCES app.candidate_document(document_id) ON DELETE CASCADE; -- 3. candidate_license.source_document_id ALTER TABLE app.candidate_license DROP CONSTRAINT IF EXISTS candidate_license_source_document_id_fkey; ALTER TABLE app.candidate_license ADD CONSTRAINT candidate_license_source_document_id_fkey FOREIGN KEY (source_document_id) REFERENCES app.candidate_document(document_id) ON DELETE CASCADE; -- 4. candidate_language.source_document_id ALTER TABLE app.candidate_language DROP CONSTRAINT IF EXISTS candidate_language_source_document_id_fkey; ALTER TABLE app.candidate_language ADD CONSTRAINT candidate_language_source_document_id_fkey FOREIGN KEY (source_document_id) REFERENCES app.candidate_document(document_id) ON DELETE CASCADE; -- 5. candidate_experience.source_document_id ALTER TABLE app.candidate_experience DROP CONSTRAINT IF EXISTS candidate_experience_source_document_id_fkey; ALTER TABLE app.candidate_experience ADD CONSTRAINT candidate_experience_source_document_id_fkey FOREIGN KEY (source_document_id) REFERENCES app.candidate_document(document_id) ON DELETE CASCADE;