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 models.py file which is already created in django app folder. My django app folder's name is “main”.

    main

    Note

    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.

    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 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);  
    Note 

    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 manage.py file.

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

    code
    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 manage.py makemigrations <write your django app name>”.

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

    cmd

    This command create migration folder in django app folder that have file 0001_initial.py 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 manage.py sqlmigrate <write your django app name> <Migration file prefix number number>”

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

    cmd

    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 manage.py migrate”,

    code

    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:

    database

    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 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 command.

Issues:

  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 models.py 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.