Fixed Django migrate No changes detected not being able to create tables
- 2020-10-23 20:09:18
- OfStack
The cause of
After modifying the table structure, python3 manage. py migrate error:
django.db.utils.OperationalError: (1091, "Can't DROP 'email'; check that column/key exists")
So I go into the database and Delete the corresponding table, and I want to regenerate this table.
After deleting the table:
python3 manage.py makemigrations
python3 manage.py migrate
Still cannot generate table, hint :No changes detected
The process
First, the files and cache files corresponding to the database under the corresponding directory of app were deleted:
$ rm -rf migrations/ __pycache__/
Re-execute:
$ python3 manage.py makemigrations
No changes detected
$~/code/django/blogproject$ python3 manage.py makemigrations comments
Migrations for 'comments':
comments/migrations/0001_initial.py
- Create model Comment
$~/code/django/blogproject$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, comments, contenttypes, sessions, users
Running migrations:
No migrations to apply.
Enter the database and still no tables are generated.
Then I found an django_migrations table, which recorded the record of creating the table, and deleted the corresponding data table:
delete from django_migrations where app='yourappname';
Re-execute the generate database command:
$ python3 manage.py makemigrations comments
No changes detected in app 'comments'
$~/code/django/blogproject$ python3 manage.py migrate comments
Operations to perform:
Apply all migrations: comments
Running migrations:
Applying comments.0001_initial... OK
The data table was generated successfully.
conclusion
In the implementation
python3 manage.py makemigrations python3 manage.py migrate
When you do this, not only will you create the model script for 0001_initial.py, but you will also create a model for creating a database record. If you want to rebuild the database, you need to delete both places.