Django makemigrations vs migrate explained

Why do we run BOTH makemigrations and migrate in Django? When I started learning Django, this was one of the concepts that confused me the most. If Django already knows our models, why do we need two commands? Here’s the simple explanation Suppose we create or modify a model: class Student(models.Model): name = models.CharField(max_length=100) Now we need to update the database so that it contains a Student table. But Django doesn’t change the database directly. Instead, it uses a migration system to track database changes safely. Step 1️⃣ makemigrations python manage.py makemigrations This command checks your models and creates migration files that describe what changed. Examples of changes it tracks: ✔ Create a new table ✔ Add a new field ✔ Remove a field ✔ Modify a field These instructions are stored inside the migrations folder. Think of it as: “Prepare the instructions for updating the database.” Step 2️⃣ migrate python manage.py migrate This command reads the migration files and applies those changes to the database. It actually: ✔ Creates tables ✔ Updates columns ✔ Applies schema changes Now your database structure matches your models. 📌 Simple way to remember makemigrations → prepare instructions migrate → execute instructions 💡 Bonus Tip Whenever you modify your models, always run: makemigrations → migrate to keep your database and models in sync. Understanding this small concept makes working with Django models much easier. #Django #Python #BackendDevelopment #LearningInPublic #WebDevelopment

To view or add a comment, sign in

Explore content categories