Solve the problem that the database table cannot be migrated when the database table is deleted in the django project

When using django today, I accidentally deleted a table in the database, and then no matter how I migrated, I couldn't migrate out of the table specified in the models. Even if the 0001_initial.py migration file in migrations is deleted and migrated again, even if the migration is successful, the table cannot be generated.

solution:

1. Find the django_migrations table in the database. If not, it will be automatically generated after all migrations in the terminal:

python manage.py migrate

2. Find the migration record corresponding to the table you deleted in the table, delete the app migration record, and save the table.

3. Re-migrate the database, although an error will be reported, but this must be available , if you forget this step, see step 6

manage.py migrate XXXapp 

At this point it will prompt:

django.db.utils.OperationalError: (1050, "XXX' already exists"), half the battle here.
4. Add --fake at the end of the migration command and try again

python manage.py migrate XXXapp --fake

5. The table is regenerated.

6. If still unsuccessful, as prompted:

No migrations to apply.

You can try to delete the migration records in migrations, and re-migrate (makemigrations), and then perform the above operations from scratch.

Guess you like

Origin blog.csdn.net/qq_58174484/article/details/124318196