Skip to content
Sarj Code Standards

new-table-inline-constraints

Declare new-table constraints inside CREATE TABLE.

Why

Splitting an empty table's primary key, uniqueness, foreign keys, and checks into later statements hides its complete integrity contract and adds avoidable ALTER TABLE operations.

Fix

Move these constraints into the original CREATE TABLE, preserving their names and options. A plain unique column index can use a named UNIQUE constraint. Keep ordinary, partial, expression, and concurrent indexes separate; PostgreSQL cannot express those as equivalent table constraints.

Examples

Before — flagged The new table's primary key belongs in its definition
supabase/migrations/001_invite.sql
CREATE TABLE invite (id UUID, email TEXT);
ALTER TABLE invite
ADD CONSTRAINT invite_pkey PRIMARY KEY (id);
After — preferred Inline constraints and a separate partial expression index
supabase/migrations/001_invite.sql
CREATE TABLE invite (
id UUID PRIMARY KEY,
email TEXT,
accepted_at TIMESTAMPTZ
);
CREATE UNIQUE INDEX invite_open_email ON invite (LOWER(email))
WHERE
accepted_at IS NULL;