Templates/Social & community

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

Start with one diagram, free

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