Welcome back! In the last lessons, you learned how to set up your Flask app and design your database models for recipes, ingredients, and reviews. Now, as you continue building your AI Cooking Helper, you might find yourself needing to clear out all the data in your database. This is especially common during development and testing, when you want to start fresh or remove test data.
Resetting your database means deleting all the records from your tables. This can help you:
- Remove test or sample data before going live.
- Fix mistakes if you accidentally added bad data.
- Start over with a clean slate for new features or tests.
In this lesson, you will learn how to create a script that safely deletes all recipes
, ingredients
, and reviews
from your database. This is a powerful tool, so we will also make sure it is safe to use.
Before we dive in, let’s quickly remind ourselves how our database is set up. In a previous lesson, you created three main models using SQLAlchemy:
Recipe
Ingredient
Review
You also set up a special association table called recipe_ingredient
to handle the many-to-many relationship between recipes and ingredients. This means:
- Each recipe can have many ingredients.
- Each ingredient can be used in many recipes.
Here’s a quick look at how these models are related:
This structure is important because, when we delete data, we need to be careful about the order, especially with association tables.
Deleting all data from your database is a big step. If you run a script that wipes everything, you can’t get that data back unless you have a backup. That’s why it’s important to add a safety check before doing anything destructive.
A common way to do this is to prompt the user for confirmation. This gives you a chance to stop the script if you didn’t mean to run it.
For example, you can use Python’s input()
function to ask the user:
- The script asks the user to type
y
to continue. - If the user types anything else, the script stops and prints "Aborted."
This simple step can save you from accidentally deleting important data.
Now, let’s talk about how to actually delete the data. Because of the relationships between tables, the order in which you delete matters.
- The
recipe_ingredient
table links recipes and ingredients. If you try to delete a recipe or ingredient that is still linked in this table, you might get an error. - To avoid this, you should delete the links in the association table first, then delete the main records.
-
Delete from the association table first:
This removes all links between recipes and ingredients.
-
Delete from dependent tables:
Review.query.delete()
removes all reviews.Recipe.query.delete()
removes all recipes.Ingredient.query.delete()
removes all ingredients.
-
Commit the changes:
This saves all the deletions to the database.
By following this order, you avoid errors and make sure all data is removed cleanly.
Let’s build the script step by step so you can see how each part works.
First, you need to import the necessary modules and set up the Flask app context. This allows you to access the database from your script.
sys.path.insert(...)
temporarily adds the path to your app’s directory to Python’s list of places to look for modules. This allows you to import your app’s code even when running the script from outside the main project folder.from main import create_app
imports the function to create your Flask app.from app.models import ...
imports your models and the database object.
Next, add the safety prompt we discussed earlier:
app = create_app()
creates the Flask app.with app.app_context():
lets you work with the database.- The prompt asks the user to confirm before continuing.
Now, add the code to delete the data in the correct order:
- The script prints a message before deleting.
- It deletes from the association table first, then the other tables.
- After committing, it prints a success message.
Finally, add the code to run the function if the script is executed directly:
This lets you run the script from the command line.
If you run the script, you might see:
If you type anything other than y
, you’ll see:
In this lesson, you learned how to safely reset your cooking app’s database by:
- Adding a user confirmation prompt to prevent accidental data loss.
- Deleting data in the correct order to avoid errors with relationships.
- Using a script to automate the process and provide clear feedback.
This script is a helpful tool during development and testing. In the next practice exercises, you’ll get hands-on experience writing and running your own reset scripts. This will help you become more comfortable managing your database as you continue building your AI Cooking Helper.
