V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
rationa1cuzz
V2EX  ›  Python

django 对于数据已存在数据的表增加非空字段如何处理?

  •  
  •   rationa1cuzz · 2021-01-13 14:45:13 +08:00 · 1455 次点击
    这是一个创建于 1170 天前的主题,其中的信息可能已经有所发展或是发生改变。
    对于已经存在数据的表增加非空字段,makemigrations 会报错 You are trying to add a non-nullable field 'xx' to XXX without a default,有什么办法可以解决吗?还是说必须要给个默认值
    5 条回复    2021-01-14 11:51:28 +08:00
    wuwukai007
        1
    wuwukai007  
       2021-01-13 15:10:39 +08:00
    null=True,blank=True
    xpresslink
        2
    xpresslink  
       2021-01-13 15:10:57 +08:00
    表增加非空字段必须要加 default:默认值,这可以是值或可调用对象。如果可调用,则每次创建新对象时都会调用它。
    enomine
        3
    enomine  
       2021-01-13 15:13:38 +08:00
    报错提示已经很明显了
    从 MySQL 层面来讲,非空表加一列非空字段,如果没有设置默认值,那已经存在的那些数据新加这个字段应该填充什么数据呢?
    IurNusRay
        4
    IurNusRay  
       2021-01-14 09:05:32 +08:00
    要么设置 null=True,要么手动设置一个默认值,比如 0 或者""
    msy45
        5
    msy45  
       2021-01-14 11:51:28 +08:00
    如果不需要默认值,migrate 完再把默认值取消再 migrate 一次
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1176 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:27 · PVG 02:27 · LAX 11:27 · JFK 14:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.