Skip to content
thesarfo

Note

Django Migrations

Creating and running migrations to keep the database schema in sync with your models.

views 0

Creating Migrations

In Django, migrations are used to create or update our database tables based on the models in our project. We dont manually create or modify database tables, but we let Django take care of that.

Terminal window
python manage.py makemigrations

What happens is that Django takes a look at all our apps inside our project, and then creates a new migration file for each app. A migration file is simply a python module that contains information about our db. At the appropriate time, when you run the migrations against a db, django will convert the code in the migrations.py file into SQL code and run it on top of the db.

As you build new models, or modify existing ones, you need to run the “makemigrations” command to create a new migration file, ie updating it.

Running Migrations

We have to run our migrations in order to translate it into a database schema. We use the command “python manage.py migrate” to do that. Python goes through all our installed apps, and then executes all pending migrations.

Now if you look into your project directory you will see a file named “db.sqlite3” which is a very small and lightweight db that should only be used for small websites. There is a vscode extension called SQLite that can help you view the db contents.