Get to Know Database Migration Better In Less Than One Minute : Django

Irfan Maulana Nasution
3 min readMay 3, 2021

--

Jump to the “Database Migration” for the promised “Less Than Two Minutes”

Table of Contents

  • Table of Contents
  • Introduction
  • Database Migration
  • Automatic Data Seeding
  • TL;DR

Introduction

I have used django for a long time and use “makemigrations” and “migrate” many times. but for all that time i don’t really understand what that command is for. all i know is if i have a database and want it to run i need to run that command.

recently I’ve been diving to the documentation about the migration on the conquest to make team coding_convention and debugging some problem. while i learn a lot more than what i write in this article i feel like these is what the fundamental of migration in django that need to be understood by newbie such as me. So here it is

Database Migration

In Django (or in any MVC web framework) we make our database with “Model”. Model as it name said is simply the model of the actual database. so there is a need to implement this model to the actual database which is what migration is

Model is a blueprint of a database. Migration is the act of implementing the blueprint to the actual database

Migration in Django

Makemigrations is making the migration preparation. You will have migrations folder in your app after you run this command

python manage.py makemigrations

Migrate is the actual “migration”. it transfer the database information (metadata etc) to the actual database.

python manage.py migrate

Django Migration Notes

  • you should do makemigrations before migrate
  • you cant use anything related with the database before you migrate
  • you should not put any line of code that is using the database outside function or class. it would prevent you from running makemigrations and migrate
  • deleting migrations folder might be what you need in case of database/migration error

Automatic Data Seeding

In some cases you need the database to have some data as soon as the app is deployed. In example you make a news web app. and there is fixed data like news-topic : business, traveling, fashion. you need to create these object. with automatic data seeding you can initiate the database on the deployment. First prepare you init data in a file like JSON/XML/YAML and put it in ROOT_DIR/fixture/

#ROOT_DIR/app/fixture/activity.json
[
{
"model": "content.activity",
"fields": {
"id_activity": "AT1",
"name_activity": "Menyusun Balok",
"category" : "Motorik",
"for_age" : "18 Bulan" },
{
"model": "content.activity",
"fields": {
"id_activity": "AT2",
"name_activity": "Bermain dengan boneka",
"category" : "Sosial",
"for_age" : "18 Bulan" }
}]

and then run this command

>>> in  : python manage.py loaddata activity.json
>>> out : Installed 2 object(s) from 1 fixture(s)

After you successfully load the data. the data can be seen in your database by accessing admin page. You will see 2 objects of activity already added. just as if you put it 2 objects manually.

TL;DR

Model is a blueprint of a database. Migration is the act of implementing the blueprint to the actual database

Automatic data seeding is initiating data to the database (without manually inputting the data)

--

--

Irfan Maulana Nasution

Your everyday software engineer. Ex average student. Always striving to be excelent and what im working on. And this is where I share my thoughts and experience