推荐学习书目
› 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 · Jan 13, 2021 · 2754 views
    This topic created in 2133 days ago, the information mentioned may be changed or developed.
    对于已经存在数据的表增加非空字段,makemigrations 会报错 You are trying to add a non-nullable field 'xx' to XXX without a default,有什么办法可以解决吗?还是说必须要给个默认值
    5 replies  •  2021-01-14 11:51:28 +08:00
    wuwukai007
    wuwukai007
    Jan 13, 2021
    1
    null=True,blank=True
    xpresslink
    xpresslink
    Jan 13, 2021
    2
    表增加非空字段必须要加 default:默认值,这可以是值或可调用对象。如果可调用,则每次创建新对象时都会调用它。
    enomine
    enomine
    Jan 13, 2021
    3
    报错提示已经很明显了
    从 MySQL 层面来讲,非空表加一列非空字段,如果没有设置默认值,那已经存在的那些数据新加这个字段应该填充什么数据呢?
    IurNusRay
    IurNusRay
    Jan 14, 2021
    4
    要么设置 null=True,要么手动设置一个默认值,比如 0 或者""
    msy45
    msy45
    Jan 14, 2021
    5
    如果不需要默认值,migrate 完再把默认值取消再 migrate 一次
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4459 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:36 · PVG 16:36 · LAX 00:36 · JFK 03:36
    ♥ Do have faith in what you're doing.