Introduction
In this chapter, we will start discussing the migration of the Database model.
Migration of Database Model
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.
- Now open the models.py file which is already created in the Django app folder. My Django app folder's name is “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.
- After open models.py and write code as below,
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:
- CREATE TABLE "main_userprofile" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name"
- varchar(30) NOT NULL, "address" varchar(50) NOT NULL, "city" varchar(60) NOT NULL
- , "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.
- 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.
- Django uses the SQLite database by default in the configuration of settings.py file.
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.
- After opening the Command Prompt write the following command:
- python manage.py makemigrations <write your django app name>
Ex: “python manage.py makemigrations main”.main is my Django app name.
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 the model. I will tell you in the Next tutorials.
- After that write the following 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.
This above image shows a query of table creation that has an ID field automatically generated. This command makes a table query.
- The last step is for making a table in a database; write the following command:
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 has many features in Django which already predefine in it.
When I open the SQLite database you will see these tables as below image:
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 the 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:
- python manage.py makemigrations
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: - Don’t delete migration folder files. Otherwise, models lose their connectivity to the database.
- No need for creating the ID field in each table. Django creates it automatically.
- 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.
Summary
In the next chapter, we will continue learning the migration of database models.