SoFunction
Updated on 2024-11-16

django resynchronization after deleting a database table

Due to project needs , recently in a Python language based on a back-end framework Django web application development . I have to say, Django inherited Python's simplicity, use it to develop web applications simple and refreshing, unlike the former SSH framework, you need to configure each framework separately, you also need to configure the various frameworks set up, Django on the integration of the SSH three frameworks, you only need to configure the framework, the entire back-end development process can be completed, and the configuration process is easy to learn. Configuration process is simple and easy to learn, which greatly reduces the complexity of the use of programmers , you can focus more energy on writing good code , rather than entangled in the use of tools .

Without further ado, let's now talk about the problem I encountered and the solution.

We all know that Django provides ORM functionality to directly create database tables as well as perform additions, deletions, modifications and checks by manipulating classes in the code. However, in the development process, due to the redesign of the database table, you need to delete the original table and resynchronize the table through Django's ORM functionality.

The synchronization commands are as follows:

python  makemigrations 
python  migrate 

But it reported an error after I entered the command:

The prompt said that the field I added did not have a default value (my modification to the database table was to add a field), and then I went to Baidu and found the answer on *, which was to add a default value after the field. But obviously this answer didn't find the real cause of my problem, because this way the first command was executed successfully, but when I got to the second one, I got an error.

Then I went to Baidu, someone suggested to look at the framework automatically generated initial file, the file in the current app under the migrations directory, open the file found that the contents of the file and the original table corresponds to the original table, that is, there is no update, the problem may be here. So I deleted the file and regenerated it as suggested.

After deleting the file and re-executing the command, this time it did generate a new initial file and the content was updated, but still something went wrong when executing the second command, checking the database, it was empty, no new tables were generated. Crashed.

Then went back to Baidu, looking for a related question to read, and saw another command:

python  sqlmigrate your_app_name 0001 

Replace your_app_name with your own app name to see the create table sql statement automatically generated by the framework, so that's how I saw the sql statement. I executed that sql command directly in the database to create the table manually, and then started the app, it could start normally, problem solved.

Although this method is a bit tricky, I guess, and still doesn't seem to explain what the problem is, it's good that it solves the problem in a very practical way.

The above method of resynchronization after deleting database tables in this django is all that I have shared with you.