Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.8 - what's the difference between migrate and makemigrations?

According to the documentation here: https://docs.djangoproject.com/en/1.8/topics/migrations/ it says:

migrate, which is responsible for applying migrations, as well as unapplying and listing their status. 

and

makemigrations, which is responsible for creating new migrations based on the changes you have made to your models. 

From what I understand, I first do

makemigrations 

to create the migration file and then do

migrate 

to actually apply the migration?

Do note though that I just began my Django project and I added my app to my "installed_apps" list. After that, I did

python manage.py runserver 

and it said

You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them. 

It didn't mention anything about running makemigrations.

like image 345
SilentDev Avatar asked May 01 '15 01:05

SilentDev


People also ask

What is the difference between Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

What is migration and migrate in Django?

Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They're designed to be mostly automatic, but you'll need to know when to make migrations, when to run them, and the common problems you might run into.

What is use of Makemigrations command in Django?

Django app model makemigrations makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps' model which you add in installed apps. It does not execute those commands in your database file.

Is Django migration necessary?

Migrations are not required. They can be useful for creating and tracking database changes via code, but Django applications will run properly without them.


2 Answers

According the Polls tutorial:

  1. python manage.py makemigrations <app>: Create the migrations (generate the SQL commands).

  2. python manage.py migrate: Run the migrations (execute the SQL commands).

like image 135
Hakim Avatar answered Sep 29 '22 15:09

Hakim


As Django's documentation says Migrations are Django’s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema.

makemigrations basically generates the SQL commands for preinstalled apps (which can be viewed in installed apps in settings.py) and your newly created apps' model which you add in installed apps.It does not execute those commands in your database file. So tables doesn't created after makemigrations.

After applying makemigrations you can see those SQL commands with sqlmigrate which shows all the SQL commands which has been generated by makemigrations.

migrate executes those SQL commands in database file.So after executing migrate all the tables of your installed apps are created in your database file.

You can conform this by installing sqlite browser and opening db.sqlite3 you can see all the tables appears in the database file after executing migrate command.

like image 43
Rajat Bhatt Avatar answered Sep 29 '22 16:09

Rajat Bhatt