CREATE TABLE "portfolio_tag" (
	"id" bigserial PRIMARY KEY NOT NULL,
	"portfolio_id" bigint NOT NULL,
	"name" text NOT NULL,
	"colour" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "property_tag" (
	"property_id" bigint NOT NULL,
	"tag_id" bigint NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "property_tag_property_id_tag_id_pk" PRIMARY KEY("property_id","tag_id")
);
--> statement-breakpoint
ALTER TABLE "portfolio_tag" ADD CONSTRAINT "portfolio_tag_portfolio_id_portfolio_id_fk" FOREIGN KEY ("portfolio_id") REFERENCES "public"."portfolio"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "property_tag" ADD CONSTRAINT "property_tag_property_id_property_id_fk" FOREIGN KEY ("property_id") REFERENCES "public"."property"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "property_tag" ADD CONSTRAINT "property_tag_tag_id_portfolio_tag_id_fk" FOREIGN KEY ("tag_id") REFERENCES "public"."portfolio_tag"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "uq_portfolio_tag_portfolio_lower_name" ON "portfolio_tag" USING btree ("portfolio_id",lower("name"));--> statement-breakpoint
CREATE INDEX "ix_property_tag_tag" ON "property_tag" USING btree ("tag_id");