Templates/SaaS & accounts

Audit log database schema

Who did what to which record, with before and after values.

3 tables, 2 relationships

Opens as a new diagram you can change. Free.

Tables

actors

  • iduuid not null
  • kindtext not null
  • display_nametext not null
  • external_idtext
  • created_attimestamptz not null

audit_events

  • iduuid not null
  • actor_id> actorsuuid
  • actiontext not null
  • ip_addressinet
  • occurred_attimestamptz not null
  • created_attimestamptz not null

audit_targets

  • iduuid not null
  • event_id> audit_eventsuuid not null
  • target_typetext not null
  • target_idtext not null
  • beforejsonb
  • afterjsonb
  • 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 "actors" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "kind" TEXT NOT NULL,
  "display_name" TEXT NOT NULL,
  "external_id" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "audit_events" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "actor_id" UUID,
  "action" TEXT NOT NULL,
  "ip_address" INET,
  "occurred_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "audit_targets" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "event_id" UUID NOT NULL,
  "target_type" TEXT NOT NULL,
  "target_id" TEXT NOT NULL,
  "before" JSONB,
  "after" JSONB,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "audit_events" ADD CONSTRAINT "fk_audit_events_actor_id" FOREIGN KEY ("actor_id") REFERENCES "actors" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "audit_targets" ADD CONSTRAINT "fk_audit_targets_event_id" FOREIGN KEY ("event_id") REFERENCES "audit_events" ("id") ON DELETE CASCADE 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.