解决方法:
添加链接描述
https://stackoverflow.com/questions/26185687/you-are-trying-to-add-a-non-nullable-field-new-field-to-userprofile-without-a
models.Model
You are trying to add a non-nullable field ‘aaa’ to post without a default; we can’t do that (the database needs something to populate existing rows).
Please select a fix:
- Provide a one-off default now (will be set on all existing rows with a null value for this column)
- Quit, and let me add a default in models.py
You need to provide a default value:
new_field = models.CharField(max_length=140, default=‘SOME STRING’)
rm your_app/migrations/*
rm db.sqlite3
python manage.py makemigrations
python manage.py migrate
ou can’t add reference to table that have already data inside.
Change:
user = models.OneToOneField(User)
to:
user = models.OneToOneField(User, default = “”)
do:
python manage.py makemigrations
python manage.py migrate
change again:
user = models.OneToOneField(User)
do migration again:
python manage.py makemigrations
python manage.py migrate
本文详细介绍了在数据库迁移过程中,如何解决添加非空字段时遇到的问题,包括提供默认值、使用一对一字段的正确方式以及如何进行多次迁移来避免数据丢失。
3万+

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



