Generador Visual de Esquemas y RLS para Supabase

Construye tablas relacionales visualmente y copia el script SQL exacto con pgcrypto, índices automáticos en FKs y políticas base de Row Level Security.

Plantillas:
organizations5 cols
id
uuid
name
varchar
slug
varchar
plan
varchar
created_at
timestamp
users5 cols
id
uuid
organization_id
uuid
email
varchar
role
varchar
is_active
boolean
subscriptions6 cols
id
uuid
organization_id
uuid
provider_id
varchar
status
varchar
price_cents
int
renews_at?
timestamp
audit_logs5 cols
id
serial
user_id?
uuid
action
varchar
metadata?
jsonb
created_at
timestamp
Arrastra las tablas para organizar el diagrama · Haz clic en una tabla para editar sus columnas
-- Generated by SchemaCost (Supabase + RLS)
CREATE EXTENSION IF NOT EXISTS "pgcrypto";

CREATE TABLE "organizations" (
  "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "name" VARCHAR(255) NOT NULL,
  "slug" VARCHAR(255) NOT NULL UNIQUE,
  "plan" VARCHAR(255) NOT NULL DEFAULT 'free',
  "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE "users" (
  "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "organization_id" UUID NOT NULL REFERENCES "organizations"("id") ON DELETE CASCADE,
  "email" VARCHAR(255) NOT NULL UNIQUE,
  "role" VARCHAR(255) NOT NULL DEFAULT 'member',
  "is_active" BOOLEAN NOT NULL DEFAULT true
);

CREATE TABLE "subscriptions" (
  "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "organization_id" UUID NOT NULL UNIQUE REFERENCES "organizations"("id") ON DELETE CASCADE,
  "provider_id" VARCHAR(255) NOT NULL UNIQUE,
  "status" VARCHAR(255) NOT NULL,
  "price_cents" INTEGER NOT NULL,
  "renews_at" TIMESTAMPTZ
);

CREATE TABLE "audit_logs" (
  "id" BIGSERIAL PRIMARY KEY,
  "user_id" UUID REFERENCES "users"("id") ON DELETE SET NULL,
  "action" VARCHAR(255) NOT NULL,
  "metadata" JSONB,
  "created_at" TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Foreign Key Indexes
CREATE INDEX "idx_users_organization_id" ON "users" ("organization_id");
CREATE INDEX "idx_subscriptions_organization_id" ON "subscriptions" ("organization_id");
CREATE INDEX "idx_audit_logs_user_id" ON "audit_logs" ("user_id");

-- Supabase Row Level Security (Starter Policies)
ALTER TABLE "organizations" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "authenticated_read_organizations" ON "organizations" FOR SELECT TO authenticated USING (true);
ALTER TABLE "users" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "authenticated_read_users" ON "users" FOR SELECT TO authenticated USING (true);
ALTER TABLE "subscriptions" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "authenticated_read_subscriptions" ON "subscriptions" FOR SELECT TO authenticated USING (true);
ALTER TABLE "audit_logs" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "authenticated_read_audit_logs" ON "audit_logs" FOR SELECT TO authenticated USING (true);

Ventajas clave

  • ✓ Agrega automáticamente índices en llaves foráneas para evitar consultas lentas.
  • ✓ Incluye plantillas listas para SaaS Multi-Tenant, E-Commerce y Fintech.

Preguntas frecuentes

¿Por qué es importante indexar llaves foráneas en PostgreSQL / Supabase?

PostgreSQL no crea índices automáticos en columnas FOREIGN KEY; agregarlos evita escaneos completos en JOINs y borrados en cascada.