[django automatic database synchronization] Django synchronizes the database after changing apps

Django deletes the database table and resynchronizes

Original July 3, 2017 10:25:53

label:

python/

django

1811

Due to project needs, a back-end framework based on the Python language Django has recently been used to develop web applications. It must be said that Django inherits the simplicity of Python, and using it to develop web applications is simple and refreshing. Unlike the previous SSH framework, each framework needs to be configured separately, and the various frameworks need to be assembled through configuration. Django integrates SSH The functions of the three frameworks, only need to configure this framework, the entire back-end development process can be completed, and the configuration process is easy to learn, which greatly reduces the complexity of programmers and can focus more on writing Produce good code, not entangled in the use of tools.

Without further ado, let me talk about the problems and solutions I have encountered.

We all know that Django provides the ORM function, you can directly create database tables and add, delete, modify and check through the classes in the operation code. However, in the development process, due to the redesign of the database table, you need to delete the original table and re-synchronize the table through the Django ORM function.

The synchronization commands are as follows:

[plain]viewplaincopy

python?manage.py?makemigrations

[plain]viewplaincopy

python?manage.py?migrate

But after I entered the command I got an error:

The prompt says that there is no default value for the new field I added (my modification to the database table is to add a field), then I went to Baidu and found the answer on stackoverflow, just add a default value after the field. But obviously this answer did not find the real cause of the problem I encountered, because the first command was successfully executed in this way, but the error was reported directly when the second command was executed. Then I went to Baidu again. Someone suggested to take a look at the initial file automatically generated by the framework. This file is in the migrations directory under the current app. After opening, it is found that the file content corresponds to the original table, that is, it has not been updated. The problem may be here Too. So I deleted this file and regenerated as suggested.

After deleting the file, re-execute the command. This time, a new initial file was indeed generated and the content has been updated, but there was still a problem when executing the second command. Looking at the database, it was empty and no new table was generated. collapse.

Then I went to Baidu again to find related questions and saw another command:

[plain]viewplaincopy

python?manage.py?sqlmigrate?your_app_name?0001

Replace your_app_name with your own app name to see the SQL statement created by the framework, so I saw the SQL statement. I directly executed the sql command in the database to manually create the table, and then start the application, it can start normally and the problem is solved.

Although this method is a bit tricky, it seems that there is no explanation for the crux of the problem, but fortunately, the problem is solved very practically.

Guess you like

Origin www.cnblogs.com/sqlserver-mysql/p/12712871.html