I want to drop constraint with the name unknown so I use the following code:
EXECUTE 'ALTER TABLE public."IntravenousTherapyAppointment" DROP CONSTRAINT '||fk_Name||';';
The problem is that fk_Name ends with '~' so I get syntax error.
The full code:
DO $$
DECLARE fk_Name TEXT;
BEGIN
fk_Name := (SELECT
tc.constraint_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name='IntravenousTherapyAppointment' AND ccu.table_name='MedicamentMeasurementUnit');
IF fk_Name IS NOT NULL THEN
EXECUTE 'ALTER TABLE public."IntravenousTherapyAppointment" DROP CONSTRAINT '||fk_Name||';';
END IF;
END $$;