【Django】python manage.py makemigrations & python manage.py migrate command answers and solutions that cannot create new tables

1. Detailed explanation of commands

1. Act globally

python manage.py makemigrations means to inform Django about what to do, and to make a record, but it doesn't actually do it

python manage.py migrate  actually starts creating the database

2. Act on app or file

It should be noted that these two commands act globally by default , that is, for all the latest changed models or

The migration file under migrations performs corresponding operations. If you want to only work on some apps, execute the following command:

python manage.py makemigrations appname

There is an additional 001_initial file under the makemigrations command file

The contents of this file indicate that we have created a model class Project, and pointed out the member attributes id, name, etc. and definitions of this class, and created model classes in models.py, one of which corresponds to a data table. This command does not actually add the database table

python manage.py migrate appname

 Looking at the database at this time, the project table has been created in mysql

If you want to be precise to a certain migration file, you can use:

python manage.py migrate appname 0001_initial (filename)

 2. Answers to questions

Because the data table has been generated before, or the command is executed again after modifying the content, it is found that it cannot be generated, for example:

Solution:

1. Delete the 001_initial file

2. Delete the records in the django_migrations table

Since a table django_migrations that comes with Django has added the previously generated table, such as

3. Re-execute the above two commands

python manage.py makemigrations appname

python manage.py migrate appname

Other bugs:

models.E028::: The same table name is used in multiple Model's db_table

1. Set managed=False in the meta class in the appname/models.py file

2. During operation, the model with managed=true will be detected to see whether multiple models use the same table name.

 

 

Guess you like

Origin blog.csdn.net/Next_SummerAgain/article/details/131610183