Django中關於MySQL的bug總結


bug one:

You are trying to add a non-nullable field 'height' to person 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)
2) Quit, and let me add a default in models.py

問題描述:當數據庫表已經建立完成之后,再往models.py該表格所對應的類中再次添加新的字段,然后執行python manage.py makemigrations時,就會產生該bug。

具體原因:當我們創建表完成后,Django會默認覺得我們已經往表中添加了數據。當添加了一個新的字段的時候,表中已經存在的記錄也都同時多了這個新添加的字段,但是賦給這個字段什么值Django不知道怎么辦了,所以我們需要給這個新字段添加一個默認值。

解決辦法:

1.

You are trying to add a non-nullable field 'height' to person 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)
2) Quit, and let me add a default in models.py
Select an option: 2   這里選擇2 

2.剛才新添加的字段給個默認值。default='默認值'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM