CREATE TABLE IF NOT EXISTS "documenttemplates" ( "id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, "created_at" timestamptz NOT NULL DEFAULT now(), "tenant" bigint NOT NULL REFERENCES "tenants"("id"), "name" text NOT NULL, "document_type" text NOT NULL, "template_data" jsonb NOT NULL DEFAULT '{}'::jsonb, "default" boolean NOT NULL DEFAULT false, "archived" boolean NOT NULL DEFAULT false, "updated_at" timestamptz, "updated_by" uuid REFERENCES "auth_users"("id"), "created_by" uuid REFERENCES "auth_users"("id") ); CREATE INDEX IF NOT EXISTS "documenttemplates_tenant_type_idx" ON "documenttemplates" ("tenant", "document_type");