Migration Of Database Models
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”.
- After open models.py and write code as below,
- 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.- 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);
- Where ->
- 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.
- After opening the Command Prompt write the following command:Ex: “python manage.py makemigrations main”.main is my Django app name.
- python manage.py makemigrations <write your django app name>
- After that write the following command:EX: “python manage.py sqlmigrate main 0001”. Here 0001 denote prefix of 0001_initial.py file name.
- python manage.py sqlmigrate <write your Django app name> <Migration file prefix number>
- The last step is for making a table in a database; write the following command:
- python manage.py migrate
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
- 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:
- 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.
Author
Neeraj Kumar
0
8.1k
2.3m