Templates/Commerce

Food delivery database schema

Customers order from restaurants; couriers pick up, deliver and get rated.

6 tables, 5 relationships

Opens as a new diagram you can change. Free.

Tables

customers

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

restaurants

  • iduuid not null
  • nametext not null
  • latitudenumeric(9,6) not null
  • longitudenumeric(9,6) not null
  • created_attimestamptz not null

couriers

  • iduuid not null
  • nametext not null
  • vehicletext not null
  • availableboolean not null
  • created_attimestamptz not null

orders

  • iduuid not null
  • customer_id> customersuuid not null
  • restaurant_id> restaurantsuuid not null
  • delivery_addresstext not null
  • totalnumeric(10,2) not null
  • statustext not null
  • created_attimestamptz not null

deliveries

  • iduuid not null
  • order_id> ordersuuid not null
  • courier_id> couriersuuid
  • picked_up_attimestamptz
  • delivered_attimestamptz
  • created_attimestamptz not null

ratings

  • iduuid not null
  • order_id> ordersuuid not null
  • food_ratinginteger not null
  • courier_ratinginteger
  • commenttext
  • 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 "couriers" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "vehicle" TEXT NOT NULL,
  "available" BOOLEAN DEFAULT false NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "customers" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "phone" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "deliveries" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "order_id" UUID NOT NULL UNIQUE,
  "courier_id" UUID,
  "picked_up_at" TIMESTAMPTZ,
  "delivered_at" TIMESTAMPTZ,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "orders" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "customer_id" UUID NOT NULL,
  "restaurant_id" UUID NOT NULL,
  "delivery_address" TEXT NOT NULL,
  "total" NUMERIC(10,2) NOT NULL,
  "status" TEXT DEFAULT 'placed' NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "ratings" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "order_id" UUID NOT NULL UNIQUE,
  "food_rating" INTEGER NOT NULL,
  "courier_rating" INTEGER,
  "comment" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "restaurants" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "latitude" NUMERIC(9,6) NOT NULL,
  "longitude" NUMERIC(9,6) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "orders" ADD CONSTRAINT "fk_orders_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "orders" ADD CONSTRAINT "fk_orders_restaurant_id" FOREIGN KEY ("restaurant_id") REFERENCES "restaurants" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "deliveries" ADD CONSTRAINT "fk_deliveries_order_id" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "deliveries" ADD CONSTRAINT "fk_deliveries_courier_id" FOREIGN KEY ("courier_id") REFERENCES "couriers" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "ratings" ADD CONSTRAINT "fk_ratings_order_id" FOREIGN KEY ("order_id") REFERENCES "orders" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

More Commerce templates

Start with one diagram, free

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