Migrations Introduction 🎯

beginner
16 min

Migrations Introduction 🎯

Welcome to our Django Migrations tutorial! In this lesson, we'll explore what migrations are, why they're essential for Django projects, and how to create, apply, and unapply migrations. Let's get started!

What are Migrations? πŸ“

In Django, migrations are files that describe the current state of your database schema, including tables, columns, and relationships. They help manage database changes as you develop your application.

Why are Migrations Important? πŸ’‘

Migrations are essential for several reasons:

  1. Version Control: Migrations allow you to track and control the evolution of your database schema over time.
  2. Safety: Django automatically creates and applies migrations for you, ensuring that your database schema is always in a consistent state.
  3. Automatic Backups: Django automatically creates and stores backup migrations, allowing you to revert to previous versions of your database schema if needed.

Creating a New App and Initial Migration βœ…

Let's create a new Django project and an app called blog to illustrate migrations in action.

  1. Create a new Django project:
django-admin startproject mysite cd mysite
  1. Create a new app called blog:
python manage.py startapp blog
  1. Open the blog/models.py file and define a simple Post model:
python
from django.db import models class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() published_date = models.DateTimeField('date published')
  1. To create the initial migration, run:
python manage.py makemigrations blog

Applying a Migration βœ…

After creating the initial migration, we need to apply it to our database:

python manage.py migrate

Now, Django has created the Post table in our database.

Making Changes to the Model βœ…

Let's make a change to our Post model:

python
class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() published_date = models.DateTimeField('date published') author = models.ForeignKey('auth.User', on_delete=models.CASCADE)

We've added an author field to the Post model. To create a new migration that reflects these changes, run:

python manage.py makemigrations blog

Applying the Updated Migration βœ…

After creating the updated migration, we can apply it to our database:

python manage.py migrate

Now, the Post table has been updated with the new author field.

Rolling Back a Migration βœ…

You can roll back a migration by using the migrate command with the --fake option. Let's roll back to the previous state of the Post model:

python manage.py migrate blog zero
Quick Quiz
Question 1 of 1

If you want to roll back to a previous state of the `Post` model, what command should you use?

That's it for our Migrations Introduction lesson! In the next lesson, we'll dive deeper into managing migrations, exploring advanced topics like managing multiple databases and customizing migrations. Stay tuned! 🎯