Okay, looking at the schema you provided:
-- public.organization_user_map definition
-- Drop table
-- DROP TABLE public.organization_user_map;
CREATE TABLE public.users ( -- <--- Table is named 'users' here
user_id uuid NOT NULL,
organization_id int2 NOT NULL,
display_name text NULL,
theme_preference varchar(10) DEFAULT 'system'::character varying NULL,
CONSTRAINT organization_user_map_pkey PRIMARY KEY (user_id), -- <--- Constraint name implies 'organization_user_map'
CONSTRAINT organization_user_map_user_uid_key UNIQUE (user_id) -- <--- Constraint name implies 'organization_user_map'
);
-- Table Triggers
create trigger organization_id_trigger before
update
on
public.organization_user_map for each row execute function prevent_organization_id_update(); -- <--- Trigger is on 'organization_user_map'
-- public.organization_user_map foreign keys
ALTER TABLE public.organization_user_map ADD CONSTRAINT organization_user_map_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id); -- <--- FK is on 'organization_user_map'
ALTER TABLE public.organization_user_map ADD CONSTRAINT organization_user_map_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES public.organization(organization_id); -- <--- FK is on 'organization_user_map'
There seems to be a mix-up in the table name in your script. The CREATE TABLE statement names it public.users, but all the constraints, triggers, and comments refer to public.organization_user_map.
Assuming you intended to create the public.organization_user_map table and make user_id its primary key (which is consistent with the constraints you've already defined), here is the corrected script.
The CREATE TABLE statement already defines user_id as the primary key. The only necessary correction is to change the table name in the CREATE TABLE statement itself to match the rest of the script.
-- public.organization_user_map definition
-- Drop table
-- DROP TABLE public.organization_user_map;
CREATE TABLE public.organization_user_map ( -- Corrected table name here
user_id uuid NOT NULL,
organization_id int2 NOT NULL,
display_name text NULL,
theme_preference varchar(10) DEFAULT 'system'::character varying NULL,
-- This constraint already makes user_id the primary key
CONSTRAINT organization_user_map_pkey PRIMARY KEY (user_id),
-- This unique constraint is redundant with the primary key, but kept from your original
CONSTRAINT organization_user_map_user_uid_key UNIQUE (user_id)
);
-- Table Triggers
-- This trigger is correctly applied to organization_user_map
create trigger organization_id_trigger before
update
on
public.organization_user_map for each row execute function prevent_organization_id_update();
-- public.organization_user_map foreign keys
-- These foreign keys are correctly applied to organization_user_map
ALTER TABLE public.organization_user_map ADD CONSTRAINT organization_user_map_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id);
ALTER TABLE public.organization_user_map ADD CONSTRAINT organization_user_map_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES public.organization(organization_id);
In this corrected script, the table is consistently named public.organization_user_map, and the line CONSTRAINT organization_user_map_pkey PRIMARY KEY (user_id) already ensures that user_id is the primary key.