Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Prompt No changes detected when using makemigrations in django

Shulou Source: shulou.com Published: 2022-06-02 05:07:40 10月19日 Update

This article mainly explains "prompt No changes detected when using makemigrations in django". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "prompt No changes detected when using makemigrations in django".

When we use Django to create projects, we all use makemigrations and migrate to generate the original database model, but in the end we all create tables manually in the database. Why? Because these database migration commands often display No changes detected, it is clear that we have changed the database model, why the prompt has not changed? Here we need to figure out how the database migration command identifies model changes. I will not introduce the detailed source code analysis here, just talk about its process to help you understand it.

First of all, we need to know, what is related to database migration?

Models defined in models.py

Migrations directory under APP

Django_ migrations table in the database

Makemigrations

When you execute the makemigrations command, the execution process is as follows:

According to the registered APP, get all the migrations directories under the APP and traverse the py files under them (those that do not start with _ ~)

Then generate the corresponding database table model according to the contents of the file

Class Migration (migrations.Migration):

Initial = True

Dependencies = [

]

Operations = [

Migrations.CreateModel (

Name='Course'

Fields= [

('id', models.AutoField (auto_created=True, primary_key=True, serialize=False, verbose_name='ID'))

('title', models.CharField (max_length=64))

]

),

]

Then generate the database table model according to the classes in the models.py file

Compare the model fields generated in 2 and 3, and if there is any change, generate a new .py file in the migrations directory of the corresponding APP

Migrate

When you execute the migrate command, the execution process is as follows:

According to the registered APP, get all the migrations directories under the APP and traverse the py files under them (those that do not start with _ ~)

Generate a database table model based on all the migration files in the migrations directory

Reads all records in the django_ migrations table, which records the migration file information generated by all tables

Idappnameapplied1contenttypes0001_initial2019-09-08 09:48:47.0407542Course0001_initial2019-09-08 09 52purl 23.045994

Check whether the migration files loaded by the data model recorded in the table match the files in the actual migrations directory. If all of them have been loaded, they will not be executed. If there are any unloaded files, execute the database command.

Conclusion

Through the above analysis, the following conclusions are drawn:

There must be a migrations folder under app, even if it is empty (must be, must be, must be)

When performing a migration, you should execute makemigrations before migrate (this is a recommendation)

If the execution command shows no change, please compare the file names in the django_ migrations table and the migrations directory, whether the corresponding files and the migration have been performed

If you want to perform the migration again, for example, there is a 0002_initial.py file under migrations, and the django_migrations also contains the corresponding records, delete the corresponding records in the django_ migrations table and re-execute the migrate command.

At this point, I believe you have a deeper understanding of "prompt No changes detected when using makemigrations in django". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: File data database model command directory generation prompt change use content time process actual beginning conclusion analysis learning practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Redmi Shulou Information macOS Apple