-- Image Refs
CREATE TABLE IF NOT EXISTS public.images_refs (
    codice TEXT NOT NULL,
    ordine integer NOT NULL,
    img TEXT NOT NULL,
    thumb TEXT NOT NULL,
    og_url TEXT NOT NULL
);
-- Unique Codice and Ordine
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1
    FROM pg_constraint
    WHERE conname = 'img_cod_order'
    ) THEN
        ALTER TABLE public.images_refs
        ADD CONSTRAINT img_cod_order UNIQUE (codice, ordine);
    END IF;
END $$;
-- Foreign Key to Annunci
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'cod_image'
    ) THEN
        ALTER TABLE public.images_refs 
        ADD CONSTRAINT cod_image 
        FOREIGN KEY (codice) REFERENCES public.annunci (codice) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;