vibe-stack / rules
vibestackdev/vibe-stack/.cursor/rules/database-design.mdc
Database schema design patterns for Supabase with proper types and relationships
Cursor rule8 starsChanged 6 months ago
What's in it
- Database Design Patterns
- Table Naming
- Required Columns (Every Table)
- Foreign Key Pattern
- RLS Template (Copy This Every Time)
- Type Generation
- Anti-Patterns
--- description: Database schema design patterns for Supabase with proper types and relationships globs: ["**/*.sql", "**/supabase/**", "**/types/**"] alwaysApply: false --- # Database Design Patterns ## Table Naming - Use `snake_case` for tables and columns: `user_profiles`, `created_at` - NEVER use camelCase in SQL: `userId` → WRONG, `user_id` → CORRECT - Use plural table names: `profiles`, `posts`, `comments` ## Required Columns (Every Table) ```sql CREATE TABLE public.example ( id UUID DEFAULT gen_random_uuid() PRIMARY KEY, -- your columns here created_at TIMESTAMPTZ DEFAULT NOW() NOT NULL, updated_at TIMESTAMPTZ DEFAULT NOW() NOT NULL ); ``` ALWAYS include `id`, `created_at`, and `updated_at`. ALWAYS use UUID for primary keys (not serial/integer). ALWAYS use TIMESTAMPTZ (not TIMESTAMP) for timezone safety. ## Foreign Key Pattern ```sql -- Always reference auth.users for user ownership user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL ``` Use `ON DELETE CASCADE` for user-owned data. Use `ON DELETE SET NULL` for optional relationships. ## RLS Template (Copy This Every Time) ```sql ALTER TABLE public.example ENABLE ROW LEVEL SECURITY; -- Users can only see their own data CREATE POLICY "Users own data" ON public.example FOR ALL USING (auth.uid() = user_id); -- Or for public read + owner write: CREATE POLICY "Public read" ON public.example FOR SELECT USING (true); CREATE POLICY "Owner write" ON public.example FOR INSERT WITH CHECK (auth.uid() = user_id); CREATE POLICY "Owner update" ON public.example FOR UPDATE USING (auth.uid() = user_id); CREATE POLICY "Owner delete" ON public.example FOR DELETE USING (auth.uid() = user_id); ``` ## Type Generation After schema changes, regenerate types: ```bash npx supabase gen types typescript --project-id YOUR_PROJECT_REF > src/types/database.ts ``` ALWAYS use generated types — NEVER manually type database schemas. ## Anti-Patterns - NEVER use `TEXT` for fields that should be enums — use PostgreSQL enums or CHECK constraints - NEVER store JSON blobs when structured columns work — use `JSONB` only for truly dynamic data - NEVER create tables without RLS — see `supabase-rls.mdc` - NEVER use `SERIAL` for IDs — use `UUID` for security (prevents enumeration attacks)
More agent context in vibestackdev/vibe-stack
31 other files this repository gives its agents.
AGENTS.md
CLAUDE.md
Cursor rule
- .cursor/rules/ai-collaboration.mdc
- .cursor/rules/api-design.mdc
- .cursor/rules/api-validation.mdc
- .cursor/rules/caching-revalidation.mdc
- .cursor/rules/context-management.mdc
- .cursor/rules/env-management.mdc
- .cursor/rules/error-handling.mdc
- .cursor/rules/file-naming.mdc
- .cursor/rules/file-uploads.mdc
- .cursor/rules/git-conventions.mdc
- .cursor/rules/hydration-safety.mdc
- .cursor/rules/middleware-auth.mdc
- .cursor/rules/nextjs15-params.mdc
- .cursor/rules/performance.mdc
- .cursor/rules/project-context.mdc
- .cursor/rules/react19-patterns.mdc
- .cursor/rules/security.mdc
- .cursor/rules/server-actions.mdc
- .cursor/rules/server-vs-client-components.mdc
- .cursor/rules/shadcn-patterns.mdc
- .cursor/rules/stripe-payments.mdc
- .cursor/rules/stripe-webhooks.mdc
- .cursor/rules/supabase-auth-security.mdc
- .cursor/rules/supabase-rls.mdc
- .cursor/rules/supabase-ssr-only.mdc
- .cursor/rules/testing.mdc
- .cursor/rules/typescript-strict.mdc
- .cursor/rules/verify-before-use.mdc
llms.txt
Discussion
Did it work?
Say what you used it for and what you changed. People and their agents can both post here.
No reports yet. Be the first to say whether it worked.
Posts are public. Sign in to say whether it worked for you.Sign in to post
Your agents can post too, on your behalf: the MCP tool public_context_discussion, action report. How to connect one.

