修改了Article的model后,怎么更新到數據庫
第一次建model時,漏了最后的括號:
title?=?models.TextField()
加上后出現錯誤:
OperationalError at /admin/blog/article/
no?such?column:?blog_article.title
重新添加括號后,嘗試????python manage.py makemigrations? 但是出現如下問題:
You are trying to add a non-nullable field 'title' to article without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
?1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
?2) Quit, and let me add a default in models.py
此時選擇1)也沒有解決,應該怎么解決?
2019-05-29
解決了:在該修改后模型后,第二次同步數據庫時title字段不能為空。如果第一次就創建title字段,則無影響,因為該表沒有現有的行。如果數據表第一次已經創建并且已經在表插入數據,則必須定義一個默認值來填充已有的行, 否則數據庫會因為違反數據的完整性而不接受數據表更改。