| name | data-modeling |
| description | Design database schemas for Grey Haven multi-tenant SaaS - SQLModel models, Drizzle schema, multi-tenant isolation with tenant_id and RLS, timestamp fields, foreign keys, indexes, migrations, and relationships. Use when creating database tables. |
| skills | ["code-style","database-conventions"] |
| allowed-tools | ["Read","Write","MultiEdit","Bash","Grep","Glob","TodoWrite"] |
Grey Haven Data Modeling Standards
Design database schemas for Grey Haven Studio's multi-tenant SaaS applications using SQLModel (FastAPI) and Drizzle ORM (TanStack Start) with PostgreSQL and RLS.
Multi-Tenant Principles
CRITICAL: Every Table Requires tenant_id
export const users = pgTable("users", {
id: uuid("id").primaryKey().defaultRandom(),
tenant_id: uuid("tenant_id").notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
updated_at: timestamp("updated_at").defaultNow().notNull(),
});
class User(SQLModel, table=True):
__tablename__ = "users"
id: UUID = Field(default_factory=uuid4, primary_key=True)
tenant_id: UUID = Field(foreign_key="tenants.id", index=True)
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
Naming Conventions
ALWAYS use snake_case (never camelCase):
email_address: text("email_address")
created_at: timestamp("created_at")
is_active: boolean("is_active")
tenant_id: uuid("tenant_id")
emailAddress: text("emailAddress")
createdAt: timestamp("createdAt")
Standard Fields (Required on All Tables)
id: uuid("id").primaryKey().defaultRandom()
created_at: timestamp("created_at").defaultNow().notNull()
updated_at: timestamp("updated_at").defaultNow().notNull()
tenant_id: uuid("tenant_id").notNull()
deleted_at: timestamp("deleted_at")
Core Tables
1. Tenants Table (Root)
export const tenants = pgTable("tenants", {
id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(),
slug: text("slug").notNull().unique(),
is_active: boolean("is_active").default(true).notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
updated_at: timestamp("updated_at").defaultNow().notNull(),
});
class Tenant(SQLModel, table=True):
__tablename__ = "tenants"
id: UUID = Field(default_factory=uuid4, primary_key=True)
name: str = Field(max_length=255)
slug: str = Field(max_length=100, unique=True)
is_active: bool = Field(default=True)
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
2. Users Table (With Tenant Isolation)
export const users = pgTable("users", {
id: uuid("id").primaryKey().defaultRandom(),
tenant_id: uuid("tenant_id").notNull(),
email_address: text("email_address").notNull().unique(),
full_name: text("full_name").notNull(),
is_active: boolean("is_active").default(true).notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
updated_at: timestamp("updated_at").defaultNow().notNull(),
deleted_at: timestamp("deleted_at"),
});
export const usersTenantIndex = index("users_tenant_id_idx").on(users.tenant_id);
class User(SQLModel, table=True):
__tablename__ = "users"
id: UUID = Field(default_factory=uuid4, primary_key=True)
tenant_id: UUID = Field(foreign_key="tenants.id", index=True)
email_address: str = Field(max_length=255, unique=True)
full_name: str = Field(max_length=255)
is_active: bool = Field(default=True)
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
deleted_at: Optional[datetime] = None
Relationships
One-to-Many
export const posts = pgTable("posts", {
id: uuid("id").primaryKey().defaultRandom(),
tenant_id: uuid("tenant_id").notNull(),
user_id: uuid("user_id").notNull(),
title: text("title").notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));
export const postsRelations = relations(posts, ({ one }) => ({
user: one(users, {
fields: [posts.user_id],
references: [users.id],
}),
}));
Many-to-Many
export const user_roles = pgTable("user_roles", {
id: uuid("id").primaryKey().defaultRandom(),
tenant_id: uuid("tenant_id").notNull(),
user_id: uuid("user_id").notNull(),
role_id: uuid("role_id").notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
});
export const userRolesUserIndex = index("user_roles_user_id_idx").on(user_roles.user_id);
export const userRolesRoleIndex = index("user_roles_role_id_idx").on(user_roles.role_id);
RLS Policies
Enable RLS on All Tables
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
CREATE POLICY "tenant_isolation"
ON users
FOR ALL
USING (tenant_id = current_setting('app.tenant_id')::uuid);
CREATE POLICY "admin_override"
ON users
FOR ALL
TO admin_role
USING (true);
Indexes
Required Indexes
export const usersTenantIndex = index("users_tenant_id_idx").on(users.tenant_id);
export const postsUserIndex = index("posts_user_id_idx").on(posts.user_id);
export const postsCompositeIndex = index("posts_tenant_user_idx")
.on(posts.tenant_id, posts.user_id);
Migrations
Drizzle Kit
bun run db:generate
bun run db:migrate
Alembic (SQLModel)
alembic revision --autogenerate -m "add users table"
alembic upgrade head
alembic downgrade -1
Supporting Documentation
All supporting files are under 500 lines per Anthropic best practices:
When to Apply This Skill
Use this skill when:
- Creating new database tables
- Designing multi-tenant data models
- Adding relationships between tables
- Creating RLS policies
- Generating database migrations
- Refactoring existing schemas
- Implementing soft deletes
- Adding indexes for performance
Template Reference
These patterns are from Grey Haven's production templates:
- cvi-template: Drizzle ORM + PostgreSQL + RLS
- cvi-backend-template: SQLModel + PostgreSQL + Alembic
Critical Reminders
- tenant_id: Required on EVERY table (no exceptions!)
- snake_case: All fields use snake_case (NEVER camelCase)
- Timestamps: created_at and updated_at on all tables
- Indexes: Always index tenant_id and foreign keys
- RLS policies: Enable RLS on all tables for tenant isolation
- Soft deletes: Use deleted_at instead of hard deletes
- Foreign keys: Explicitly define relationships
- Migrations: Test both up and down migrations
- Email fields: Name as email_address (not email)
- Boolean fields: Use is_/has_/can_ prefix