Templates/SaaS & accounts

SaaS starter database schema

Organisations, users and memberships for a multi-tenant app.

4 tables, 4 relationships

Opens as a new diagram you can change. Free.

Tables

organizations

  • iduuid not null
  • nametext not null
  • slugtext not null
  • created_attimestamptz not null

users

  • iduuid not null
  • emailtext not null
  • nametext
  • created_attimestamptz not null

memberships

  • iduuid not null
  • organization_id> organizationsuuid not null
  • user_id> usersuuid not null
  • rolemember_role not null
  • created_attimestamptz not null

invitations

  • iduuid not null
  • organization_id> organizationsuuid not null
  • emailtext not null
  • rolemember_role not null
  • invited_by_id> usersuuid
  • expires_attimestamptz not null
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TYPE "member_role" AS ENUM ('owner', 'admin', 'member');

CREATE TABLE "invitations" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "organization_id" UUID NOT NULL,
  "email" TEXT NOT NULL,
  "role" "member_role" DEFAULT 'member' NOT NULL,
  "invited_by_id" UUID,
  "expires_at" TIMESTAMPTZ NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "memberships" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "organization_id" UUID NOT NULL,
  "user_id" UUID NOT NULL,
  "role" "member_role" DEFAULT 'member' NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "organizations" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "slug" TEXT NOT NULL UNIQUE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL UNIQUE,
  "name" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "memberships" ADD CONSTRAINT "fk_memberships_organization_id" FOREIGN KEY ("organization_id") REFERENCES "organizations" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "memberships" ADD CONSTRAINT "fk_memberships_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "invitations" ADD CONSTRAINT "fk_invitations_organization_id" FOREIGN KEY ("organization_id") REFERENCES "organizations" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "invitations" ADD CONSTRAINT "fk_invitations_invited_by_id" FOREIGN KEY ("invited_by_id") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

More SaaS & accounts templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.