In the actual project development process , It's hard to avoid it because of what I wrote before model.py The model class of the file is not satisfied , Even some places need to be modified , At this time, it is often due to complex table relationships , All kinds of errors will occur when the modified model class is re migrated , for example NO migrations to apply It's one of them . However, in the help through the network , Some suggestions
perform python3 manage.py makemigrations --empty Your app name ;
perform python3 manage.py makemigrations;
perform python3 manage.py migrate;
The original link is https://www.cnblogs.com/li1992/p/10648390.html But I found that after the test , There is no solution to my problem .
And there is such a solution ,
Delete the app Under the name migrations file .
Access to database , find django_migrations Table of , Delete the app All records of names .
delete from django_migrations;
python manage.py makemigrations
python manage.py migrate
But to be honest, I haven't tried this way , The original link is https://www.cnblogs.com/mensiler/p/9455387.html But it is actually the process of deleting the migrated files , Same as the first effect .
Because I spent a long time here , Just try to go around , One way to do this is directly in a visual database ( I use it navicat) Directly create the tables and their field constraints you need ; Second, delete all the original tables , Re migration , But with the help of navicat You can easily and quickly export and import data and table relationships , stay navicat Will the app The following table structure and data export , Equivalent to backup , Then delete all these tables , It is recommended to use... In the server drop table ‘ Table name ’ To delete , Because in navicat The relationship constraint in cannot be deleted , Although I have removed the constraint , It may be a problem with the software . Be careful , Before migrating again , The original server must be first app Under the migrations Delete the original migration file in ,**( Don't delete __init__.py, Of course, the words deleted by mistake , Upload the local one again and you'll be back )** Then migrate again
python manage.py makemigrations
python manage.py migrate
At this time, because the original app Reasons for deleting the following table , No other errors will be reported , For example, when the author did not delete the table django.db.utils.InternalError: (1050, “Table ‘tb_hotnews’ already exists”), And even if a new table is created , But the loss of foreign key constraints .
Next , By re creating all the App The following tables are empty , Then re import the data and structure files exported when deleting the old tables into these new tables .
This is not really a solution , At the same time, it is not only applied to the emergence of no migrations to apply Methods , Basically, you can do this when there are various mistakes , But it is the most convenient way to find a solution in a short time , Put time and efficiency first , When the loss of deleting the table is small , It's recommended , If you have a real solution , Welcome to discuss .