Templates/Health

Nutrition & meal planning database schema

Foods with nutrients, recipes, logged meals and daily targets.

7 tables, 8 relationships

Opens as a new diagram you can change. Free.

Tables

users

  • iduuid not null
  • emailtext not null
  • created_attimestamptz not null

foods

  • iduuid not null
  • nametext not null
  • calories_per_100gnumeric(7,2) not null
  • protein_gnumeric(6,2) not null
  • carbs_gnumeric(6,2) not null
  • fat_gnumeric(6,2) not null
  • created_attimestamptz not null

recipes

  • iduuid not null
  • author_id> usersuuid
  • nametext not null
  • servingsinteger not null
  • created_attimestamptz not null

recipe_ingredients

  • iduuid not null
  • recipe_id> recipesuuid not null
  • food_id> foodsuuid not null
  • gramsnumeric(7,2) not null
  • created_attimestamptz not null

meals

  • iduuid not null
  • user_id> usersuuid not null
  • eaten_attimestamptz not null
  • kindtext not null
  • created_attimestamptz not null

meal_items

  • iduuid not null
  • meal_id> mealsuuid not null
  • food_id> foodsuuid
  • recipe_id> recipesuuid
  • gramsnumeric(7,2) not null
  • created_attimestamptz not null

daily_targets

  • iduuid not null
  • user_id> usersuuid not null
  • caloriesinteger not null
  • protein_ginteger 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 "daily_targets" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL UNIQUE,
  "calories" INTEGER NOT NULL,
  "protein_g" INTEGER NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "foods" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "calories_per_100g" NUMERIC(7,2) NOT NULL,
  "protein_g" NUMERIC(6,2) NOT NULL,
  "carbs_g" NUMERIC(6,2) NOT NULL,
  "fat_g" NUMERIC(6,2) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "meal_items" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "meal_id" UUID NOT NULL,
  "food_id" UUID,
  "recipe_id" UUID,
  "grams" NUMERIC(7,2) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "meals" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "eaten_at" TIMESTAMPTZ NOT NULL,
  "kind" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "recipe_ingredients" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "recipe_id" UUID NOT NULL,
  "food_id" UUID NOT NULL,
  "grams" NUMERIC(7,2) NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "recipes" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "author_id" UUID,
  "name" TEXT NOT NULL,
  "servings" INTEGER NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "email" TEXT NOT NULL UNIQUE,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "recipes" ADD CONSTRAINT "fk_recipes_author_id" FOREIGN KEY ("author_id") REFERENCES "users" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "recipe_ingredients" ADD CONSTRAINT "fk_recipe_ingredients_recipe_id" FOREIGN KEY ("recipe_id") REFERENCES "recipes" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "recipe_ingredients" ADD CONSTRAINT "fk_recipe_ingredients_food_id" FOREIGN KEY ("food_id") REFERENCES "foods" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

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

ALTER TABLE "meal_items" ADD CONSTRAINT "fk_meal_items_meal_id" FOREIGN KEY ("meal_id") REFERENCES "meals" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "meal_items" ADD CONSTRAINT "fk_meal_items_food_id" FOREIGN KEY ("food_id") REFERENCES "foods" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE "meal_items" ADD CONSTRAINT "fk_meal_items_recipe_id" FOREIGN KEY ("recipe_id") REFERENCES "recipes" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

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

More Health templates

Start with one diagram, free

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