修改了Article的model后,怎么更新到數(shù)據(jù)庫
第一次建model時(shí),漏了最后的括號(hào):
title?=?models.TextField()
加上后出現(xiàn)錯(cuò)誤:
OperationalError at /admin/blog/article/
no?such?column:?blog_article.title
重新添加括號(hào)后,嘗試????python manage.py makemigrations? 但是出現(xiàn)如下問題:
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
此時(shí)選擇1)也沒有解決,應(yīng)該怎么解決?
2019-05-29
解決了:在該修改后模型后,第二次同步數(shù)據(jù)庫時(shí)title字段不能為空。如果第一次就創(chuàng)建title字段,則無影響,因?yàn)樵摫頉]有現(xiàn)有的行。如果數(shù)據(jù)表第一次已經(jīng)創(chuàng)建并且已經(jīng)在表插入數(shù)據(jù),則必須定義一個(gè)默認(rèn)值來填充已有的行, 否則數(shù)據(jù)庫會(huì)因?yàn)檫`反數(shù)據(jù)的完整性而不接受數(shù)據(jù)表更改。