

{"id":61752,"date":"2019-07-06T12:59:58","date_gmt":"2019-07-06T07:29:58","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=61752"},"modified":"2021-06-21T12:19:25","modified_gmt":"2021-06-21T06:49:25","slug":"django-migrations-and-database-connectivity","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/","title":{"rendered":"Django Migrations and Database Connectivity &#8211; An Excellent concept made Easy!"},"content":{"rendered":"<p>Django is an awesome framework. It abstracts the most trivial tasks and allows for rapid development. Since Django has an active community, they are constantly adding new features to it. These features give Django an edge in web development.<\/p>\n<p>One such feature was added with the introduction of Django version 1.7, that is, Migrations which was pretty much required everywhere. It is important for defining the database schema. We also use them to maintain complex databases whenever changes are performed in them.<\/p>\n<p>We will learn about Django migrations and various other concepts related to it.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62278\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg\" alt=\"Django Migrations and Database Connectivity\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<p>Awesome!! We have so much information to grasp. So, let&#8217;s explore it.<\/p>\n<h3>Databases and Django<\/h3>\n<p><em>A database is a collection of data arranged in an efficient way. It ensures fast and secure storage, access and update of data.<\/em> That is, the main concept of every type of database is both paper registers and MySQL.<\/p>\n<p>There are many types of databases out there. We will be narrowing them down to relational databases. Now, what\u2019s that? <em>A relational database is a collection of tables having a number of columns and rows.<\/em><\/p>\n<p>Yes, there are databases that actually don\u2019t use columns and rows. So, whatever databases we have worked with on the web is a relational database. These databases can have relational tables or fields. We have used Foreign Key Field in our Django projects.<\/p>\n<p>Here, we are creating a post involving a Foreign Key for Post Table.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62303\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table.png\" alt=\"Foreign key for Post table\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Foreign-key-for-Post-table-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This is a snapshot of the <strong>models.py<\/strong> file of our project.<\/p>\n<p>SQL (Structured Query Language) is a very popular language for databases. Almost all the popular databases use SQL. The databases like PostgreSQL, MySQL, SQLite, etc. are some of many examples.<\/p>\n<p>The databases mentioned above are relational databases. Yes, the majority of SQL databases are relational and are extensively used in the industry.<\/p>\n<p>What\u2019s special about PostgreSQL, MySQL, etc. is that they have native support from Django. SQLite3 is the one that comes installed out of the box, although, you will have to install the software for other databases. Django ORM will handle the rest according to the backend engine used.<\/p>\n<p>We will discuss the configuration of these databases in a separate article. It will provide you with all the required information and Django settings. Now, let\u2019s learn some important configuration of settings for databases.<\/p>\n<h4>Configuring Settings for Django Database<\/h4>\n<p>These are common settings you should know to connect database with Django project. This code exists in the\u00a0<strong>settings.py file.<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62304\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project.png\" alt=\"Common Settings to Connect Database with Django project\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Common-Settings-to-Connect-Database-with-Django-project-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>Here is the snapshot of the default code that Django provides.<\/p>\n<p>Basically, database settings for your project reside in a<em> <strong>Python Dictionary<\/strong><\/em>. The name of the dictionary is <strong>DATABASE<\/strong>. This dictionary and some of its attributes are\u00a0pre-defined.<\/p>\n<p>The <strong>default key<\/strong> is pre-defined, which contains configuration settings for a default database. Since Django can work with multiple databases, you can have multiple database configurations.<\/p>\n<p>The default key is required to configure a single or multiple databases. Inside the default value, key resides the configuration for the database.<\/p>\n<p>The configuration can vary for different types of the database you are using.<\/p>\n<p><strong>The default value is:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">DATABASES = {\r\n    'default': {\r\n        'ENGINE': 'django.db.backends.sqlite3',\r\n        'NAME': 'mydatabase',\r\n    }\r\n}<\/pre>\n<p>Now, these keys have different values for different databases. This configuration is for SQLite database.<\/p>\n<p>The <strong>ENGINE<\/strong> <strong>Key<\/strong> is used to specify the database backend. This will connect the Django project to SQLite Database. You can use other backends too. For that, you have to provide additional keys. We will configure these databases in a separate article.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>Django comes with these four database backends natively. These databases will require their database software but the backend is in-house.<\/p>\n<p><strong>PostgreSQL:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">django.db.backends.postgresql<\/pre>\n<p><strong>MySQL:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">django.db.backends.mysql<\/pre>\n<p><strong>SQLite:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">django.db.backends.sqlite3<\/pre>\n<p><strong>Oracle:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">django.db.backends.oracle<\/pre>\n<p>These backends will be able to implement all the <em><strong>latest features of Django<\/strong><\/em>. They are developed and maintained by the <strong>DSF (Django Software Foundation)<\/strong>. You can use other backends as well. In that case, you have to import the full package path of that backend.<\/p>\n<p>Since Django has a vast community, you can get backends for all kinds of databases.<\/p>\n<p>The <strong>NAME<\/strong> <strong>Key<\/strong> contains the name of the database. For SQLite, you can directly use this database. For databases like MySQL or PostgreSQL, you need to create the database first.<\/p>\n<p>So, these were two important fields for configuring databases in Django. You can complete our <em><strong>Django Database DataFlair tutorial<\/strong><\/em> to implement the code in this tutorial.<\/p>\n<h4>How these Databases Work with Django<\/h4>\n<p>In this section, we are going to understand how these databases work with Django. After you have configured your database settings and checked that everything is working fine, see what happens under the hood.<\/p>\n<p>When we start our Django project, it loads the <strong>settings.py<\/strong> file on the server. This file contains all information and even database configurations.<\/p>\n<p>Then Django will load the Database Backend. That backend will connect to the default database configured in settings. It is done by passing appropriate key values to the Database Software.<\/p>\n<p>The database then verifies the values and the connection is established. The connection is between the Database Backend and Database. The backend becomes a mediator between Database and Django.<\/p>\n<p>All the built-in backends are very efficient and are used in a production environment.<\/p>\n<p>Now, the database is connected and we can easily migrate our models and create tables in the same. The backend also handles all the CRUD operations from Django on the database.<\/p>\n<p>Migrations were not present in Django versions before version 1.7. Let\u2019s learn how developers dealt with the situation.<\/p>\n<h4>Problems Solved by Django Migrations<\/h4>\n<p>When migrations were introduced for the first time in Django, it became a huge hit. Django\u2019s popularity grew exponentially. This proves migrations are a very important feature.<\/p>\n<p>So, when there were no migrations in Django, developers had some difficulties.<\/p>\n<p>They had to define the tables and database schemas according to models. Now, this may seem easy in listening but SQL is quite difficult. This became more complex when they had to create models with foreign key relations in them.<\/p>\n<p>This also led to occasional errors and database inconsistency. The development was much slower than it is today. The developers would also have to keep track of all the changes they made themselves which was very difficult.<\/p>\n<p>The process of reverting back was the same time-consuming experience.<\/p>\n<p>These were some of the many problems faced by developers with no migrations.<\/p>\n<p>Migrations resolved\u00a0all of them and also some additional features were provided. Let\u2019s learn in-depth about these features and solutions.<\/p>\n<h3>Django <strong>Migrations<\/strong><\/h3>\n<p>In the previous section, we learned that SQL is used to create a Database Schema. That has to be the same as Models that we defined in our application. Writing SQL with the same constraints as our Models is difficult. It requires a decent knowledge of SQL.<\/p>\n<p>Django solved this problem with the introduction of <strong>Django ORM or Object-relational Mapper<\/strong>. An ORM as its name will map the Model Fields to Database Table Fields\/ Columns.<\/p>\n<p>The model has 3 fields namely<strong> date, price, volume.<\/strong> These are defined in Python with model class attributes. Django ORM generates the table. It also defines corresponding fields and constraints.<\/p>\n<p>The example table has fields mapped to the table. There is an extra field that works as a primary key. This is an awesome feature of Django ORM. It defines a primary key automatically if we haven\u2019t defined our own. This has saved a lot of developers from database errors.<\/p>\n<p>Django ORM does this via migrations. So, what are migrations?<\/p>\n<p>Django migrations are nothing but <em><strong>Python Files<\/strong><\/em>. They contain Python code which is used by Django ORM to create and alter tables and fields in the database.<\/p>\n<p>Django implements Models and changes to them on the database via migrations. Django generates migrations automatically. This is a key feature and you should also take care of these. Migrations are not meant to be corrected in normal conditions.<\/p>\n<p>You can modify them but the file is accurate most of the time. We have used migrations every time we used a Model.<\/p>\n<h4>Key Benefits of Django Migrations<\/h4>\n<p>Migrations had a huge impact on the development process of web apps. They have greatly reduced the errors in one of the most error-prone areas by migrations.<\/p>\n<p>This has made it easier for beginners to work with a database with no experience in SQL or databases. It has also increased the utility of Django and made the developer\u2019s life easier. Some of these main benefits of migrations are as follows:<\/p>\n<ul>\n<li><strong>Creating tables without SQL<\/strong><\/li>\n<\/ul>\n<p>We have used MySQL and phpMyAdmin which is a graphical tool for databases. If we had used SQL instead, then making tables would become more complex.<\/p>\n<p>For beginner developers, this could pose a real problem. Migrations had made Django more fun to use. It lets us bypass the whole SQL writing and makes tables automatically.<\/p>\n<ul>\n<li><strong>Models and Database Schema are always synchronized<\/strong><\/li>\n<\/ul>\n<p>This was the most error-prone zone for backend developers. Since they had to make the Database Schema same as the Models. It would take complex SQL and thus, resulting in errors. Sometimes, they can be real security threats but this is not the case with migrations.<\/p>\n<p>Since migrations generate SQL automatically, it is always correct. That SQL contains the same constraints, we define in Models. We can see that SQL is generated by Django ORM in a later section.<\/p>\n<ul>\n<li><strong>Don\u2019t Repeat Yourself (DRY) philosophy<\/strong><\/li>\n<\/ul>\n<p>DRY philosophy is one of the core principles in which Django is based on. Since Django is a framework for rapid development, this feature is an essential pillar.<\/p>\n<p>Django migrations automatically generate SQL. This means developers won\u2019t need to write the same code again. Not repeating the code makes the whole development fast.<\/p>\n<ul>\n<li><strong>Easier Version Control for Database Scheme<\/strong><\/li>\n<\/ul>\n<p>Migrations are Python files. Django generates a new migration file every time we modify a model. These files are not deleted and stored inside the app directory.<\/p>\n<p>These files contain very simple codes. It is easy for a developer to understand what changes were made. This also makes it easy to revert back to previous Models.<\/p>\n<p>Django migrations have several benefits, these were some important ones. We can now learn some very basic operations on migrations. We have used most of them in our previous Django tutorials.<\/p>\n<h4>Common Django Migration Operations<\/h4>\n<p>These are some of the common Django migration operations which you must know. These commands will give you an edge in handling Database Management System (DBMS). Migration is a trending topic for Django interviews. You will surely find this knowledge worthwhile.<\/p>\n<h4>Setting up Application<\/h4>\n<p>We will make a new application to implement these operations. In your Terminal\/ PowerShell prompt, execute this command to make a new application.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py startapp Cars<\/pre>\n<p><strong>Terminal Screen Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61898\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application.png\" alt=\"setting application - django migrations\" width=\"1600\" height=\"859\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/setting-application-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>You will have to install this app in <strong>settings.py.<\/strong>\u00a0Add this app\u2019s name in <strong>INSTALLED_APPS<\/strong> list.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61899\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list.png\" alt=\"installed app list\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/installed-app-list-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>Now, in <strong>Cars\/models.py<\/strong> file paste this code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.db import models\r\n#DataFlair #DataBaseMigrations\r\n\r\nclass Specs(models.Model):\r\n    name = models.CharField(max_length = 20)\r\n    price = models.DecimalField(max_digits=8, decimal_places=2)\r\n    weight = models.PositiveIntegerField()\r\n<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61900\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars.png\" alt=\"model for cars - Django Migrations\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/model-for-cars-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>We created a very simple model for cars. There are 3 fields namely<strong> name, price, and weight<\/strong>. Now we will implement all the operations on this model one by one.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p><em>It is important that your Django project has a database configured and running. To configure database settings, please visit the <strong>Django Database tutorial<\/strong>. There you can easily set up the database and proceed with this tutorial.<\/em><\/p>\n<h5>1. Creating Migrations<\/h5>\n<p>We have done this process, every time we use a model. We will now create the migration files which will then be used by the ORM to create tables. Just run this command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py makemigrations Cars<\/pre>\n<p><strong>Terminal Screen Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62296\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1.png\" alt=\"Creating Migrations terminal screen\" width=\"1600\" height=\"859\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Creating-Migrations-terminal-screen-1-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>Thus, we have a Migration file ready. The name of this file is\u00a0<strong>0001_initial.py.<\/strong> Django automatically serializes the migration files with a numerical value. This file contains the migration class.<\/p>\n<p>Although, at our level, we need not do any modification on it.<\/p>\n<h4>2. Applying Migrations<\/h4>\n<p>We created migrations in the previous file. Now, its time to apply them. We need to understand the difference. In the previous section, we just created some Python files.<\/p>\n<p>These files will not affect the database until we apply them.<\/p>\n<p>This command will apply migrations in the database.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py migrate Cars<\/pre>\n<p><strong>Terminal Screen Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62297\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations.png\" alt=\"Applying Django migrations\" width=\"1600\" height=\"859\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations-150x81.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations-1024x550.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Applying-Django-migrations-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This command can also take the name of a specific application. Django has made it so easy for us to apply migrations.<\/p>\n<p>Migrate command can apply all the migrations at once if no parameter is given.<\/p>\n<p>It is special because it will not apply unchanged migrations. So, if developers had changed only a particular model, they can simply write migrate.<\/p>\n<p>It is important that you execute <strong>makemigrations<\/strong> before <strong>migrate<\/strong> command. Since a migration file is required before applying it to the database.<\/p>\n<p>Now, we can check our database for this table.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62299\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1.png\" alt=\"phpmyadmin car_specs\" width=\"1600\" height=\"790\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/phpmyadmin-car_specs-1-520x257.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>There we have a table of <strong>cars_specs<\/strong>. If you check the structure, you will notice one extra field &#8211; Id field.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61905\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field.png\" alt=\"id field - Django Migrations\" width=\"1600\" height=\"790\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/id-field-520x257.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This is created automatically by Django ORM for the purpose of a primary key. We can define a primary key in Models for a particular field. It\u2019s the default behaviour of Django ORM.<\/p>\n<p>Every relational database requires a primary key in a table, otherwise, your table is not created. Django creators knew this very well and thus provided us with migrations functionality.<\/p>\n<h4>3. Editing Models<\/h4>\n<p>Website growth is not linear. As the website grows, there will be changes in Database Schema. So, when we change our models, we will again execute these commands:<\/p>\n<p><strong>makemigrations<\/strong> and <strong>migrate<\/strong> in their respective order.<\/p>\n<p>It is necessary for the Model definition to match the Database Schema. Otherwise, we will get an <strong>OperationalError<\/strong>.<\/p>\n<p>Now, we will change the <strong>specs model<\/strong>. As weight is not always an integer, we change it to decimal.<\/p>\n<p>Paste this Code in <strong>cars\/models.py:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from django.db import models\r\n#DataFlair #DataBaseMigrations\r\n# Create your models here.\r\n\r\nclass Specs(models.Model):\r\n    name = models.CharField(max_length = 20)\r\n    price = models.DecimalField(max_digits=8, decimal_places=2)\r\n    weight = models.DecimalField(max_digits=7, decimal_places=3)<\/pre>\n<p><strong>Code Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61906\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models.png\" alt=\"editing models - Django Migrations\" width=\"1600\" height=\"813\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models-150x76.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models-300x152.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models-768x390.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models-1024x520.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/editing-models-520x264.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>To apply this change, we will again run the commands.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-62300\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations.png\" alt=\"database table Django migrations\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-Django-migrations-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This should be the output. Let\u2019s see the database table.<\/p>\n<h5><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61909\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table.png\" alt=\"database table\" width=\"1600\" height=\"790\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-table-520x257.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/h5>\n<p>As we can clearly see, if we had to change the same manually, it would have been a time-consuming affair.<\/p>\n<h4>4. Reverting Migrations<\/h4>\n<p>We all love the <em>undo<\/em> option. It reverts back the previous state and saves our time. Django also gives us an option to revert back. Suppose you are working on production site and made some change to the Database Schema.<\/p>\n<p>Change may not yield positive results as you were expecting. Then, it will be time-consuming to change the Models again as the previous schema. Also, this can be more prone to errors, if you do it manually. Django allows you to revert back to any previous migration. The feature is important as it will change the Database Schema. That means you don\u2019t need to rewrite anything.<\/p>\n<p>This can be easily achieved with <strong>migrate<\/strong> command. This time you don\u2019t need to perform<strong> makemigrations<\/strong> command since the file already exists.<\/p>\n<p>Now, use this command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py migrate Cars 0001<\/pre>\n<p><strong>Terminal Screen Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61910\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations.png\" alt=\"reverting migrations\" width=\"1600\" height=\"843\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations-150x79.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations-300x158.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations-768x405.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations-1024x540.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/reverting-migrations-520x274.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>When we ran this command, we passed some parameters. The first parameter is the application name. In this case, it was <strong>Cars<\/strong>. The second parameter is the <strong>migration<\/strong> <strong>file<\/strong> name.<\/p>\n<p>We passed a number 0001, then the name of the file. This is one of the cool features of Django. It will match the file with the matching string 0001. Since it is a primary key for Django, therefore there are no conflicts. You can also use a complete name, the result will be the same.<\/p>\n<p>Let\u2019s take a look at the database structure now.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61911\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure.png\" alt=\"\" width=\"1600\" height=\"790\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/database-structure-520x257.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>The weight field has again reverted back to an integer.<\/p>\n<p><strong>Note:<\/strong><\/p>\n<p>Although I compared undo operation with reverting migrations; it is not the same. When we apply another migration, it will revert back the Database Schema. That is the only part which is undone.<\/p>\n<p>Suppose, you have added some fields which were not there in previous Models. Now, when you revert back the schema changed, the data which was saved in the new fields were deleted. This is the difference. Therefore, always be vary when modifying databases.<\/p>\n<p>When we reverted from this migration, the file was not deleted. The migration files are never deleted by Django. So, you have to be careful when applying migrations again. <strong>migrate<\/strong> command will automatically apply last migrations of the app.<\/p>\n<p>You can either delete that file or apply the migration to apps specific.<\/p>\n<h4>5. Indexing all Migrations<\/h4>\n<p>When applications became too big, this operation comes in handy. It will list out all the migrations, existing in the Django project. We can be specific to applications like in\u00a0<strong>migrate<\/strong> command.<\/p>\n<p>The command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py showmigrations Cars<\/pre>\n<p><strong>Terminal Screen Display:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61913\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations.png\" alt=\"indexing migrations - Django Migrations\" width=\"1600\" height=\"858\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/indexing-migrations-520x279.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This command will show all the migrations if no parameter is given. The Cars application has 2 migration files. Their names are listed out.<\/p>\n<p>You can notice that<strong> [X]<\/strong> in front of the <strong>0001_initial<\/strong> file. The <strong>X<\/strong> represents that migrations have been applied. We just reverted the migration to 0001. The second migration is unapplied, therefore<strong> [ ]. <\/strong><\/p>\n<p>It helps to determine which migrations are applied in the current database.<\/p>\n<p>This command is mostly used when you are working in teams. Since many people are applying migrations, a developer can easily check which migrations exists and work accordingly.<\/p>\n<h4>6. Naming Migrations<\/h4>\n<p>Migrations can be a user named too. This operation will let you provide meaningful names to migration. It is used at the time of creating migrations.<\/p>\n<p>The command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py makemigrations Cars -- the name change<\/pre>\n<p>Since we are not changing anything in our models, we will delete the migration file.<\/p>\n<ol>\n<li>Go to the project directory.<\/li>\n<li>Inside it, open <strong>Cars\/migrations<\/strong> directory.<\/li>\n<li>Now, delete the<strong> 0002<\/strong> files.<\/li>\n<\/ol>\n<p>You have to do this manually.<\/p>\n<p>Now execute the above command.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61914\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions.png\" alt=\"naming migartions\" width=\"1600\" height=\"843\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions-150x79.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions-300x158.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions-768x405.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions-1024x540.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/naming-migartions-520x274.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>Now, you can check that we have a new migration named <strong>0002_Change<\/strong>. It is not applied yet.<\/p>\n<p>Django keeps track of all the changes in Models. So, if models are not changing, it will not make a new migration file with the same code. This decreases code redundancy and improves performance.<\/p>\n<p>Django keeps this information in a table in the database. The table is named as\u00a0<strong>django_migrations<\/strong>.<\/p>\n<p>This table is key to Django\u2019s perfect migration management. You can see the last applied migration in the table.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-61915\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1.png\" alt=\"show migrations - Django Migrations\" width=\"1600\" height=\"790\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1-150x74.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1-300x148.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1-768x379.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1-1024x506.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/show-migrations-1-520x257.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This table gets updated as to which migrations are applied. The <strong>showmigrations<\/strong> command checks from this table for applied migrations.<\/p>\n<p>These are all the basics that you will need to work on migrations.<\/p>\n<h4>Summary<\/h4>\n<p>Now, we can conclude that migrations are a very important feature of Django. They made rapid development possible with ease. Django provides us with many commands and operations to easily manage and apply migrations.<\/p>\n<p>We also learned that un-applying migrations are not the same as undo as the data can be lost in the process.<\/p>\n<p>Hope you enjoyed this tutorial. Comment for any queries. Keep Learning\ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django is an awesome framework. It abstracts the most trivial tasks and allows for rapid development. Since Django has an active community, they are constantly adding new features to it. These features give Django&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":62278,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[20456,20455,20457,20454],"class_list":["post-61752","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-database-working-with-django","tag-databases-and-django","tag-django-database-settings-configuration","tag-django-migrations"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django Migrations and Database Connectivity - An Excellent concept made Easy! - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn the configuration settings for Django database with its working &amp; problems solved by Django migrations, its working, benefits and common operations.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django Migrations and Database Connectivity - An Excellent concept made Easy! - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn the configuration settings for Django database with its working &amp; problems solved by Django migrations, its working, benefits and common operations.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-07-06T07:29:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django Migrations and Database Connectivity - An Excellent concept made Easy! - DataFlair","description":"Learn the configuration settings for Django database with its working & problems solved by Django migrations, its working, benefits and common operations.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/","og_locale":"en_US","og_type":"article","og_title":"Django Migrations and Database Connectivity - An Excellent concept made Easy! - DataFlair","og_description":"Learn the configuration settings for Django database with its working & problems solved by Django migrations, its working, benefits and common operations.","og_url":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-07-06T07:29:58+00:00","article_modified_time":"2021-06-21T06:49:25+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Django Migrations and Database Connectivity &#8211; An Excellent concept made Easy!","datePublished":"2019-07-06T07:29:58+00:00","dateModified":"2021-06-21T06:49:25+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/"},"wordCount":3080,"commentCount":2,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg","keywords":["Database Working with Django","Databases and Django","Django Database Settings Configuration","Django Migrations"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/","url":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/","name":"Django Migrations and Database Connectivity - An Excellent concept made Easy! - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg","datePublished":"2019-07-06T07:29:58+00:00","dateModified":"2021-06-21T06:49:25+00:00","description":"Learn the configuration settings for Django database with its working & problems solved by Django migrations, its working, benefits and common operations.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/07\/Django-Migrations.jpg","width":802,"height":420,"caption":"Django Migrations and Database Connectivity"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-migrations-and-database-connectivity\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"Django Migrations and Database Connectivity &#8211; An Excellent concept made Easy!"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/61752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=61752"}],"version-history":[{"count":15,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/61752\/revisions"}],"predecessor-version":[{"id":97550,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/61752\/revisions\/97550"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/62278"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=61752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=61752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=61752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}