当你在某个models下添加了新属性,而在随后执行makemigrations后弹出需要
1)立即提供一次性默认值
2)让我在models.py中添加一个默认值
(venv) D:\project\server>python manage.py makemigrations
You are trying to add a non-nullable field 'file_Attribution' to filemodel without a default; we can't do that (the database ne
eds something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option: 2
这时选2退出,我们在新添加的属性值内加一个 default=’ something’
添加前
file_Attribution = models.CharField(max_length=255)
添加后
file_Attribution = models.CharField(max_length=255, default='something')
然后依次执行
python manage.py makemigrations
python manage.py migrate
迁移完成后再改回
file_Attribution = models.CharField(max_length=255)
PS:貌似挺绕的,但是为了不打SQL语句,倒是还行。
本文详细介绍了在Django项目中,如何解决新增非空字段迁移时遇到的问题,提供了两种解决方案:一是提供一次性默认值,二是修改models.py添加默认值,并通过实际操作步骤展示了正确的处理流程。
784

被折叠的 条评论
为什么被折叠?



