Templates/Content & media

Photo sharing database schema

Albums of photos with tags, likes and comments.

7 tables, 8 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • usernametext not null
  • avatar_urltext
  • created_attimestamptz not null

albums

  • iduuid not null
  • owner_id> usersuuid not null
  • titletext not null
  • created_attimestamptz not null

photos

  • iduuid not null
  • album_id> albumsuuid not null
  • urltext not null
  • captiontext
  • taken_attimestamptz
  • created_attimestamptz not null

tags

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

photo_tags

  • iduuid not null
  • photo_id> photosuuid not null
  • tag_id> tagsuuid not null
  • created_attimestamptz not null

likes

  • iduuid not null
  • photo_id> photosuuid not null
  • user_id> usersuuid not null
  • created_attimestamptz not null

photo_comments

  • iduuid not null
  • photo_id> photosuuid not null
  • user_id> usersuuid not null
  • bodytext 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 "albums" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "owner_id" UUID NOT NULL,
  "title" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "likes" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "photo_id" UUID NOT NULL,
  "user_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "photo_comments" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "photo_id" UUID NOT NULL,
  "user_id" UUID NOT NULL,
  "body" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "photo_tags" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "photo_id" UUID NOT NULL,
  "tag_id" UUID NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "photos" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "album_id" UUID NOT NULL,
  "url" TEXT NOT NULL,
  "caption" TEXT,
  "taken_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "tags" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" 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,
  "username" TEXT NOT NULL UNIQUE,
  "avatar_url" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "albums" ADD CONSTRAINT "fk_albums_owner_id" FOREIGN KEY ("owner_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "photos" ADD CONSTRAINT "fk_photos_album_id" FOREIGN KEY ("album_id") REFERENCES "albums" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "photo_tags" ADD CONSTRAINT "fk_photo_tags_photo_id" FOREIGN KEY ("photo_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "photo_tags" ADD CONSTRAINT "fk_photo_tags_tag_id" FOREIGN KEY ("tag_id") REFERENCES "tags" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "likes" ADD CONSTRAINT "fk_likes_photo_id" FOREIGN KEY ("photo_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

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

ALTER TABLE "photo_comments" ADD CONSTRAINT "fk_photo_comments_photo_id" FOREIGN KEY ("photo_id") REFERENCES "photos" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "photo_comments" ADD CONSTRAINT "fk_photo_comments_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Content & media templates

Start with one diagram, free

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