28 lines
1.3 KiB
SQL
28 lines
1.3 KiB
SQL
CREATE TABLE IF NOT EXISTS "email_entity_links" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"tenant_id" bigint NOT NULL,
|
|
"message_id" uuid NOT NULL,
|
|
"entity_type" text NOT NULL,
|
|
"entity_id" bigint NOT NULL,
|
|
"linked_by" uuid,
|
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
CONSTRAINT "email_entity_links_tenant_id_tenants_id_fk"
|
|
FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id")
|
|
ON DELETE cascade ON UPDATE cascade,
|
|
CONSTRAINT "email_entity_links_message_id_email_messages_id_fk"
|
|
FOREIGN KEY ("message_id") REFERENCES "public"."email_messages"("id")
|
|
ON DELETE cascade ON UPDATE cascade,
|
|
CONSTRAINT "email_entity_links_linked_by_auth_users_id_fk"
|
|
FOREIGN KEY ("linked_by") REFERENCES "public"."auth_users"("id")
|
|
ON DELETE set null ON UPDATE cascade
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "email_entity_links_message_entity_key"
|
|
ON "email_entity_links" USING btree ("message_id", "entity_type", "entity_id");
|
|
|
|
CREATE INDEX IF NOT EXISTS "email_entity_links_entity_idx"
|
|
ON "email_entity_links" USING btree ("tenant_id", "entity_type", "entity_id");
|
|
|
|
CREATE INDEX IF NOT EXISTS "email_entity_links_message_idx"
|
|
ON "email_entity_links" USING btree ("message_id");
|