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

纵饮孤独 提交于 2019-12-03 02:38:56

问题


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.


回答1:


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




回答2:


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.




回答3:


As we know Django is an ORM (Object Relational Mapping). When we use the command:

python manage.py makemigrations [app_name]

It will generate the sql command to create the table corresponding to each class you made in models.py file. then the command:

python manage.py migrate [app_name]

will create the table in database using the commands which have been generated by makemigrations.

For example, if we make a model class-

from django.db import models

class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

The corresponding sql command after using makemigrations will be

CREATE TABLE myapp_person (
"id" serial NOT NULL PRIMARY KEY,
"first_name" varchar(30) NOT NULL,
"last_name" varchar(30) NOT NULL
);

and using above command, table will be created in the database when we use migrate.




回答4:


This is django's replacement for the old manual south way of making migrations, they can be used to catalog changes in your models and write out changes that will take place in the db.

Migrate is basically the old syncdb but it takes into account all the migrations made by makemigrations.




回答5:


You should run the command -migrate- after adding a new app under the INSTALLED APPS section in the settings.py file in order to synchronize the database state with your current set of models. Assuming you've already modified the models.py file.

When you run -makemigrations- it packages up changes to your model into individual migration files.

Normally you would first run makemigrations and then migrate.

See documentation on Django Models



来源:https://stackoverflow.com/questions/29980211/django-1-8-whats-the-difference-between-migrate-and-makemigrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!