1. 程式人生 > 實用技巧 >django 資料庫遷移成功 但是表沒有建立

django 資料庫遷移成功 但是表沒有建立

花了接近一上午的時間,終於解決了。

"""
執行完python manage.py makemigrations;python manage.py migrate;
兩條命令,沒有報錯,分別顯示如下:
python manage.py makemigrations:

Migrations for 'algofilter':
  migrations\0001_initial.py
    - Create model T15Min
    - Create model T1Day
    - Create model T1Min
    - Create model T30Min
    - Create model T5Min
    - Create model TAlgoFilter
    - Create model TAlgoindicators
    - Create model TAlgoinfo
    - Create model TAlgoInvoke
    - Create model TAlgolab
    - Create model TAlgolabcor
    - Create model TSysparam
    - Create model TUniverse
Migrations for 'hisback':
  C:\Users\ASUS\Desktop\weizhong\wzapi\hisback\migrations\0001_initial.py
    - Create model TOptim
    - Create model TOptimdetail
    - Create model TSim
    - Create model TSimrun
Migrations for 'universe':
  C:\Users\ASUS\Desktop\weizhong\wzapi\universe\migrations\0001_initial.py
    - Create model TUniverse
    - Create model TUniversedetail
    
    
python manage.py migrate:

 Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying algofilter.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying hisback.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying universe.0001_initial... OK
"""


"""
但是資料庫只存在django內建的幾張表,其他的app裡面的model表沒有新建,查了很多答案,但是都沒有成功,總結如下:

1.可能是app名沒有註冊到setting.py的install_app中
2.執行遷移命令時加上app名稱,python manage.py migrations appname
"""


"""
最終把這個問題翻譯成英文,在stackoverflow上搜到了答案:

資料model是我在公司通過資料庫反向生成的,所以在class Meta 類中會有欄位managed=False, 官方文件對這個欄位的解釋是"If False, no database table creation or deletion operations will be performed for this model.",之前建立檢視也是將這個欄位改為False,
所以在遷移到另一個數據庫的時候不會生成表,於是將managed設定為True就完美解決了這個問題。
"""