Chat / messaging database schema
Direct and group conversations, messages, attachments and read receipts.
6 tables, 7 relationships
Opens as a new diagram you can change. Free.
Tables
users
- iduuid not null
- usernametext not null
- display_nametext not null
- created_attimestamptz not null
conversations
- iduuid not null
- titletext
- is_groupboolean not null
- created_attimestamptz not null
participants
- iduuid not null
- conversation_id> conversationsuuid not null
- user_id> usersuuid not null
- joined_attimestamptz not null
- mutedboolean not null
- created_attimestamptz not null
messages
- iduuid not null
- conversation_id> conversationsuuid not null
- sender_id> usersuuid not null
- bodytext not null
- edited_attimestamptz
- created_attimestamptz not null
attachments
- iduuid not null
- message_id> messagesuuid not null
- urltext not null
- mime_typetext not null
- created_attimestamptz not null
read_receipts
- iduuid not null
- message_id> messagesuuid not null
- user_id> usersuuid not null
- read_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 TABLE "attachments" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"message_id" UUID NOT NULL,
"url" TEXT NOT NULL,
"mime_type" TEXT NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "conversations" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"title" TEXT,
"is_group" BOOLEAN DEFAULT false NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "messages" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"conversation_id" UUID NOT NULL,
"sender_id" UUID NOT NULL,
"body" TEXT NOT NULL,
"edited_at" TIMESTAMPTZ,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "participants" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"conversation_id" UUID NOT NULL,
"user_id" UUID NOT NULL,
"joined_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
"muted" BOOLEAN DEFAULT false NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "read_receipts" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"message_id" UUID NOT NULL,
"user_id" UUID NOT NULL,
"read_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "users" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"username" TEXT NOT NULL UNIQUE,
"display_name" TEXT NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
ALTER TABLE "participants" ADD CONSTRAINT "fk_participants_conversation_id" FOREIGN KEY ("conversation_id") REFERENCES "conversations" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "participants" ADD CONSTRAINT "fk_participants_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "messages" ADD CONSTRAINT "fk_messages_conversation_id" FOREIGN KEY ("conversation_id") REFERENCES "conversations" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "messages" ADD CONSTRAINT "fk_messages_sender_id" FOREIGN KEY ("sender_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "attachments" ADD CONSTRAINT "fk_attachments_message_id" FOREIGN KEY ("message_id") REFERENCES "messages" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "read_receipts" ADD CONSTRAINT "fk_read_receipts_message_id" FOREIGN KEY ("message_id") REFERENCES "messages" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "read_receipts" ADD CONSTRAINT "fk_read_receipts_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;More Social & community templates
- Social networkProfiles, follows, posts with media, comments and likes.
- Discussion forumCategories, threads and posts with reactions and moderation reports.
- Dating appProfiles with photos, swipes, mutual matches, messages and blocks.
- Meetup groupsInterest groups with members, events at venues and RSVPs.
- Q&A siteQuestions, answers with votes, accepted answers and tags.
- Reviews & ratingsBusinesses reviewed by users, with photos, helpful votes and owner replies.
- Creator membershipsCreators offering paid tiers, supporters' pledges and members-only posts.
- GamificationPoints ledger, levels, badges and streaks for an engaged community.
- Mentorship programmeProgrammes matching mentors with mentees, sessions and feedback.
- Chat app (Firestore)Users, rooms with their members, and messages as a subcollection of each room.
- Activity feed (Cassandra)Each user's feed by time bucket, follower lists both ways, and like counters.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.