CREATE TABLE IF NOT EXISTS public.user_etichette (
    "userId" UUID NOT NULL,
    "etichettaId" integer NOT NULL,
);

DO $$ BEGIN
ALTER TABLE public.user_etichette 
ADD CONSTRAINT user_etichette_pkey PRIMARY KEY ("userId", "etichettaId");
    EXCEPTION
        WHEN invalid_table_definition THEN
            RAISE NOTICE 'Primary key already exists. Ignoring...'; 
END $$;
-- Foreign Key to Chats
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'user_etichetta_fkey'
    ) THEN
        ALTER TABLE public.user_etichette 
        ADD CONSTRAINT user_etichetta_fkey 
        FOREIGN KEY ("userId") REFERENCES public.users (id) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;
-- Foreign Key to Etichette
DO $$ BEGIN IF NOT EXISTS (
    SELECT 1 FROM pg_constraint 
    WHERE conname = 'etichetta_user_etichetta_fkey'
    ) THEN
        ALTER TABLE public.user_etichette 
        ADD CONSTRAINT etichetta_user_etichetta_fkey 
        FOREIGN KEY ("etichettaId") REFERENCES public.etichette (id_etichetta) 
        ON UPDATE CASCADE ON DELETE CASCADE;
    END IF;
END $$;