起因
同步表的過程中熔酷,我手動將數(shù)據(jù)庫中的一個表刪除了爽室,此時再去執(zhí)行命令温自,發(fā)現(xiàn)不能再數(shù)據(jù)庫中新建表了
修改了表結(jié)構(gòu)以后執(zhí)行python3 manage.py migrate 報錯:
No changes detected
所以進(jìn)數(shù)據(jù)庫把對應(yīng)的表刪除了,想著重新生成這張表.
刪除表以后執(zhí)行:
python3 manage.py makemigrations
python3 manage.py migrate
還是不能生成表,提示:No changes detected
處理過程
-
首先刪除了app對應(yīng)目錄下的數(shù)據(jù)庫對應(yīng)的文件和緩存文件:
$ rm -rf migrations/ __pycache__/
重新執(zhí)行:
$ 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.
-
然后帘靡,刪除app下面目錄migrations下面除了init.py其他的所有文件
-
最后知给,刪除migrations中關(guān)于你的app的同步數(shù)據(jù)數(shù)據(jù)庫記錄
delete from django_migrations where app='yourappname';
重新執(zhí)行生成數(shù)據(jù)庫命令:
$ 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
數(shù)據(jù)表順利生成.
如果提示:
django.db.utils.InternalError: (1050, "Table 'autotest_ap_kaoji' already exists")
則:
[root@HD1F-LAB dsm]# python manage.py migrate --fake <appname>
結(jié)論
在執(zhí)行
python3 manage.py makemigrations
python3 manage.py migrate
操作的時候,不僅會創(chuàng)建0001_initial.py對應(yīng)的模型腳本,還會創(chuàng)建一個數(shù)據(jù)庫記錄創(chuàng)建的模型.如果想重新生成數(shù)據(jù)庫,需要三個地方都做刪除.