I recommend reading these two posts. They should give you a good start.
Database structure for Recipes with RecipeGuides
I need help with organizing the database structure. The idea is as follows: a recipe has a cooking guide (the guide has fields: step_text and image). I was wondering how to make it most readable and correct? At first I thought to make an intermediate table: recipes -> guides -> guide_step, but then I realized that the guides table will be redundant, as I will not store any additional information about the guide. Now I have an idea to just make a One-To-Many relationship and it will appear that the recipe has many cooking steps (and the model will have the name GudieStep). What do you think?Wouldn't the c One-To-Many option be too cumbersome, and wouldn't it be weird to store a bunch of steps for different recipes in one table? Maybe someone has some better ideas?
If you have only one guide for each recipe, which sounds logical, and this guide does not contain any additional information independent of the recipe: its own name, text, images, tags, etc., for example, different chefs prepare this recipe differently. Then, indeed, you can do without a table of guides.
And no, it won't be cumbersome, it's normal practice. The steps of different recipes are stored in one table, they are separated by the recipe they belong to (recipe_id key).
MySQL tables may look like this:
CREATE TABLE recipes (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT
);
CREATE TABLE guide_steps (
id INT AUTO_INCREMENT PRIMARY KEY,
recipe_id INT NOT NULL,
step_number INT NOT NULL,
step_text TEXT NOT NULL,
image VARCHAR(255),
UNIQUE(recipe_id, step_number), -- Ensure step order is unique per recipe
FOREIGN KEY (recipe_id) REFERENCES recipes(id) ON DELETE CASCADE
);
Insert data example:
INSERT INTO recipes (title, description) VALUES
('Pasta Carbonara', 'Classic Italian dish');
INSERT INTO guide_steps (recipe_id, step_number, step_text, image) VALUES
(1, 1, 'Boil water and add pasta.', 'step1.jpg'),
(1, 2, 'Fry bacon in a pan.', 'step2.jpg'),
(1, 3, 'Mix eggs and cheese.', 'step3.jpg');
Please or to participate in this conversation.