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

后端 未结 8 851
说谎
说谎 2021-01-30 16:23

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

migrate, which is responsible for applying migrations, as          


        
8条回答
  •  萌比男神i
    2021-01-30 16:54

    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.

提交回复
热议问题