Python Django Tutorial: Migration Of Database Models - Part One

In the Python Django framework, Migration of Database Model is used for making database tables using model in Django. Django makes the tables of models through python code in Django. When user creates direct table in database without Django code then Django doesn’t give all predefined features to that table. But when creating tables through Django model then it gives many features like: create, retrieve, update and delete records and also gives the facility to add this table model in admin panel.

First create django project. If you don’t know how to create a django project follow this URL.

After successful creation of django project now we going to create models step.

  1. Now open the file which is already created in django app folder. My django app folder's name is “main”.



    In python language indentation is very important, if you not follow indentation of code your project or program will not run. You will get this error most times in python. So always use an editor that knows python language indentation otherwise use 4 spaces below of this symbol colon (:) in any editor. Example is shown in below snapshot.


  2. After open and write code as below,


    Remember the Indentation from class.

    • Where ->

      • from django.db import models define a subclass of model
      • UserProfile denotes the table name.
      • class attributes define database fields like : name, address, city, state, country
      • models.Charfield denote varchar type of data field
      • max_length= is argument which specifies the size of VARCHAR data field.
      • Django have many type of data field and many type of argument which make tables according to type.

    We can make any type of model from django code. Django also has  very Short techniques that make models flexible.

    This table create database query will be as shown below:

    1. CREATE TABLE "main_userprofile" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name"   
    2. varchar(30) NOT NULL"address" varchar(50) NOT NULL"city" varchar(60) NOT NULL  
    3. "state" varchar(30) NOT NULL"country" varchar(50) NOT NULL);  

    Django always creates ID field in tables always with “NOT NULL PRIMARY KEY AUTOINCREMENT”. So if you have any need for it declare ID field according to you.
  3. Now we go for further process. Open Command Prompt with directory of root folder of project where you can see file.

  4. Django uses SQLite database by default in configuration of file.

    We can use any database with django like: MySQL, MSSQL, Oracle and postgreslq. Django works better with PostgreSql because it have some featured libraries inbuilt for this database.

  5. After open Command Prompt write command : “python makemigrations <write your django app name>”.

    Ex: “python makemigrations main”.main is my django app name.


    This command create migration folder in django app folder that have file that contain UserProfile model definition. Here 0001 is important for further use in model. I will tell you in Next tutorials.

  6. After Again write command : “python sqlmigrate <write your django app name> <Migration file prefix number number>”

    EX: “python sqlmigrate main 0001”. Here 0001 denote prefix of file name.


    This above image show query of table creation that have ID field automatic generated. This command makes table query.

  7. Last step is for making table in database; write command : “python migrate”,


    This command creates table in database .When you type this command you will see in second line.

    Apply all migrations : main, admin, auth, contenttypes, session -> these are the inbuilt model in django that create tables automatic when you type this command first time. There table have many features in django which already predefine in it.

    When I open sqlite database you will see these tables as below image:


    Here “main_userprofile” is table name of UserProfile Model in django. Here “main” django app name.You can change it by.

Now you have Successfully migrated Django model. So then we can change this model's attributes like adding or removing attributes then run always two command.

Above all commands use the first time when you create first-time tables in database using django. But after you can only use:

  1. Python makemigrations
  2. Python migrate

Both commands are used always when you edit models. If you want to add another model then use both these commands. There is no requirement of other command.


  1. Don’t delete migration folder files. Otherwise models lose their connectivity to database.

  2. No need for creating ID field in each table. Django creates it automatically.

  3. If you want to delete table in database. Remove table model code in and run above both command then table will delete from database. If you delete table manually that will create problem with model. So don’t  manually delete database table.