BEGIN;

DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'fk_recordings_project_flock_kandang'
    ) THEN
        ALTER TABLE recordings
            DROP CONSTRAINT fk_recordings_project_flock_kandang;
    END IF;
END $$;

ALTER TABLE recordings
    ADD CONSTRAINT fk_recordings_project_flock_kandang
        FOREIGN KEY (project_flock_kandangs_id)
        REFERENCES project_flock_kandangs (id)
        ON DELETE CASCADE ON UPDATE CASCADE;

COMMIT;
