Notifications database schema
Notification types, per-user preferences, an inbox and delivery attempts.
5 tables, 5 relationships
Opens as a new diagram you can change. Free.
Tables
users
- iduuid not null
- emailtext not null
- created_attimestamptz not null
notification_types
- iduuid not null
- keytext not null
- descriptiontext not null
- created_attimestamptz not null
notification_preferences
- iduuid not null
- user_id> usersuuid not null
- type_id> notification_typesuuid not null
- channeltext not null
- enabledboolean not null
- created_attimestamptz not null
notifications
- iduuid not null
- user_id> usersuuid not null
- type_id> notification_typesuuid not null
- payloadjsonb not null
- read_attimestamptz
- created_attimestamptz not null
deliveries
- iduuid not null
- notification_id> notificationsuuid not null
- channeltext not null
- statustext not null
- sent_attimestamptz
- errortext
- 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 TABLE "deliveries" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"notification_id" UUID NOT NULL,
"channel" TEXT NOT NULL,
"status" TEXT DEFAULT 'queued' NOT NULL,
"sent_at" TIMESTAMPTZ,
"error" TEXT,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "notification_preferences" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"user_id" UUID NOT NULL,
"type_id" UUID NOT NULL,
"channel" TEXT NOT NULL,
"enabled" BOOLEAN DEFAULT true NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "notification_types" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"key" TEXT NOT NULL UNIQUE,
"description" TEXT NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "notifications" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"user_id" UUID NOT NULL,
"type_id" UUID NOT NULL,
"payload" JSONB NOT NULL,
"read_at" TIMESTAMPTZ,
"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,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
ALTER TABLE "notification_preferences" ADD CONSTRAINT "fk_notification_preferences_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "notification_preferences" ADD CONSTRAINT "fk_notification_preferences_type_id" FOREIGN KEY ("type_id") REFERENCES "notification_types" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "notifications" ADD CONSTRAINT "fk_notifications_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "notifications" ADD CONSTRAINT "fk_notifications_type_id" FOREIGN KEY ("type_id") REFERENCES "notification_types" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "deliveries" ADD CONSTRAINT "fk_deliveries_notification_id" FOREIGN KEY ("notification_id") REFERENCES "notifications" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;More SaaS & accounts templates
- SaaS starterOrganisations, users and memberships for a multi-tenant app.
- Authentication & sessionsUsers with linked identities, sessions, password resets and MFA factors.
- Roles & permissionsRole-based access control: roles, permissions and who holds them.
- Subscription billingCustomers, plans and prices, subscriptions, invoices and payments.
- Usage-based billingMeters, usage events and rate cards that roll up into invoices.
- Feature flagsFlags per environment, targeting rules and per-user overrides.
- Audit logWho did what to which record, with before and after values.
- API platformDeveloper apps with API keys, webhooks and usage records.
- Product analyticsUsers, sessions and tracked events with properties, plus saved funnels.
- Sessions and rate limits (DynamoDB)Sign-in sessions and per-key request counters, each removed by DynamoDB's TTL when it lapses.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.