Python Django Tutorial: Migration Of Database Models - Part One

Introduction

 
In the Python Django framework, the Migration of Database Model is used for making database tables using the model in Django. Django makes the tables of models through python code in Django. When a user creates a direct table in the database without Django code then Django doesn’t give all predefined features to that table. But when creating tables through the 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 the admin panel.
 
First, create a Django project. If you don’t know how to create a Django project follow this URL.
 
After the successful creation of the Django project now we going to create models step.
  1. Now open the models.py file which is already created in the Django app folder. My Django app folder's name is “main”.
     
    main
     
    Note
     
    In python language indentation is very important, if you not follow the 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. An example is shown in the below snapshot.
     
    cmd
     
  2. After open models.py and write code as below,
     
    code
     
    Remember the Indentation from class.
     
    • Where ->
       
      • from django.db import models define a subclass of a model
      • UserProfile denotes the table name.
      • class attributes define database fields like name, address, city, state, country
      • models.Charfield denotes the varchar type of data field
      • max_length= is an argument that specifies the size of the VARCHAR data field.
      • Django has any type of data field and any type of argument which makes tables according to type.
    We can make any type of model from the 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);  
    Note 
     
    Django always creates an ID field in tables always with “NOT NULL PRIMARY KEY AUTOINCREMENT”. So if you have any need for it declare the ID field according to you. 

  3. Now we go for further process. Open Command Prompt with the directory of the root folder of the project where you can see manage.py file. 
     
  4. Django uses the SQLite database by default in the configuration of settings.py file.
     
    code
     
    We can use any database with Django like MySQL, MSSQL, Oracle, and PostgreSQL. Django works better with PostgreSQL because it has some featured libraries inbuilt for this database.
     
  5. After open Command Prompt write command: “python manage.py makemigrations <write your django app name>”.
     
    Ex: “python manage.py makemigrations main”.main is my Django app name.
     
    cmd
     
     
    This command creates a migration folder in a Django app folder that has file 0001_initial.py that contain UserProfile model definition. Here 0001 is important for further use in model. I will tell you in the Next tutorials.
     
  6. After Again write command: “python manage.py sqlmigrate <write your Django app name> <Migration file prefix number>”
     
    EX: “python manage.py sqlmigrate main 0001”. Here 0001 denote prefix of 0001_initial.py file name.
     
    cmd
     
    This above image shows a query of table creation that has an ID field automatically generated. This command makes a table query.
     
  7. The last step is for making table in a database; write command: “python manage.py migrate”,
     
    code
     
    This command creates a table in the database . When you type this command you will see in the second line.
     
    Apply all migrations: main, admin, auth, contenttypes, session -> these are the inbuilt model in Django that create tables automatically when you type this command first time. Their table have many features in Django which already predefine in it.
     
    When I open the SQLite database you will see these tables as below image:
     
    database
     
    Here “main_userprofile” is the table name of the 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 commands.
 
Above all commands use the first time when you create first-time tables in a database using Django. But after you can only use:
  1. Python manage.py makemigrations
  2. Python manage.py 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 commands.
 
Issues:
  1. Don’t delete migration folder files. Otherwise, models lose their connectivity to the database.
     
  2. No need for creating the ID field in each table. Django creates it automatically.
     
  3. If you want to delete a table in the database. Remove table model code in models.py and run above both commands then the table will delete from the database. If you delete the table manually that will create a problem with the model. So don’t manually delete the database table.


Recommended Free Ebook
Similar Articles