Design complete database schemas with tables, relationships, constraints, and indexes for Supabase. Triggers when user describes data models, entities, or requests schema design.
Design comprehensive, normalized database schemas for Supabase applications.
Create well-structured database schemas following best practices for normalization, relationships, constraints, and indexing.
Gather Requirements
Design Tables
Map Relationships
Add Indexes
Implement RLS
Generate Migration
-- Users and Posts Schema
-- =======================
CREATE TABLE public.users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT UNIQUE NOT NULL,
username TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
CONSTRAINT username_length CHECK (char_length(username) >= 3)
);
CREATE TABLE public.posts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
author_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
content TEXT NOT NULL,
published BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL,
CONSTRAINT title_length CHECK (char_length(title) >= 3)
);
CREATE INDEX idx_posts_author ON public.posts(author_id);
CREATE INDEX idx_posts_published ON public.posts(published, created_at DESC)
WHERE published = true;
ALTER TABLE public.posts ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Published posts viewable by all"
ON public.posts FOR SELECT
USING (published = true);