V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · 2296 views
    This topic created in 1931 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
        1
    wuwukai007  
       Jan 13, 2021
    null=True,blank=True
    xpresslink
        2
    xpresslink  
       Jan 13, 2021
    表增加非空字段必须要加 default:默认值,这可以是值或可调用对象。如果可调用,则每次创建新对象时都会调用它。
    enomine
        3
    enomine  
       Jan 13, 2021
    报错提示已经很明显了
    从 MySQL 层面来讲,非空表加一列非空字段,如果没有设置默认值,那已经存在的那些数据新加这个字段应该填充什么数据呢?
    IurNusRay
        4
    IurNusRay  
       Jan 14, 2021
    要么设置 null=True,要么手动设置一个默认值,比如 0 或者""
    msy45
        5
    msy45  
       Jan 14, 2021
    如果不需要默认值,migrate 完再把默认值取消再 migrate 一次
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3141 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 14:36 · PVG 22:36 · LAX 07:36 · JFK 10:36
    ♥ Do have faith in what you're doing.