ALTER TABLE IF EXISTS public."Cubicle" DROP COLUMN IF EXISTS "ProviderId";
--ALTER TABLE IF EXISTS public."Cubicle" DROP CONSTRAINT IF EXISTS "FK_Cubicle_ProviderId";

ALTER TABLE IF EXISTS public."Cubicle"
    ADD COLUMN "AccountId" integer;

ALTER TABLE IF EXISTS public."Cubicle"
    ADD CONSTRAINT "FK_Cubicle_AccountId" FOREIGN KEY ("AccountId")
    REFERENCES public."Account" ("AccountId") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE CASCADE;


ALTER TABLE IF EXISTS public."Cubicle"
    ADD CONSTRAINT "FK_Cubicle_LocationId" FOREIGN KEY ("LocationId")
    REFERENCES public."Location" ("LocationId") MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE CASCADE;

--UPDATE "Appointment" set "CubicleId" = null;
--DELETE FROM "Cubicle";

CREATE SEQUENCE "AccountProviderMap_seq";

CREATE TABLE IF NOT EXISTS public."AccountProviderMap"
(
    "AccountProviderMapId" integer NOT NULL DEFAULT nextval('"AccountProviderMap_seq"'::regclass),
    "AccountId" integer,
    "ProviderId" integer,
    CONSTRAINT "AccountProviderMap_pkey" PRIMARY KEY ("AccountProviderMapId"),
    CONSTRAINT "FK_AccountProviderMap_AccountId" FOREIGN KEY ("AccountId")
        REFERENCES public."Account" ("AccountId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT "FK_AccountProviderMap_ProviderId" FOREIGN KEY ("ProviderId")
        REFERENCES public."Provider" ("ProviderId") MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS public."AccountProviderMap"
    OWNER to postgres;
	
	
	
	--select * from "Cubicle"