【发布时间】:2016-02-12 14:18:41
【问题描述】:
在开发模型时,我经常在运行makemigrations 时遇到不可空字段错误:
You are trying to add a non-nullable field 'user' to randommodel without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option:
几乎每次我收到此错误时,我都非常乐意删除该表中的数据(在开发过程中通常只有几个测试条目),并且只删除它而不是确定什么会更有效一个合适的默认值是。
但是目前我没有合适的方法来执行此操作并最终刷新数据库和/或删除迁移,这是相当繁重但有效的方法。
删除该模型/表中的数据以消除错误的最佳方法是什么? (会是通过 shell/shell_plus 吗?)
型号:
class RandomModel(models.Model):
user_details = JSONField(unique=True)
user = models.ForeignKey(User)
【问题讨论】:
标签: python django django-models