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