Booleand 字段和 Postgresql 的 IntegrityError
我有这个简单的博客模型:
class Blog(models.Model):
title = models.CharField(_('title'), max_length=60, blank=True, null=True)
body = models.TextField(_('body'))
user = models.ForeignKey(User)
is_public = models.BooleanField(_('is public'), default = True)
当我在管理界面中插入博客时,我收到此错误:
IntegrityError at /admin/blogs/blog/add/
null value in column "is_public" violates not-null constraint
为什么???
I have this simple Blog model:
class Blog(models.Model):
title = models.CharField(_('title'), max_length=60, blank=True, null=True)
body = models.TextField(_('body'))
user = models.ForeignKey(User)
is_public = models.BooleanField(_('is public'), default = True)
When I insert a blog in admin interface, I get this error:
IntegrityError at /admin/blogs/blog/add/
null value in column "is_public" violates not-null constraint
Why ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
唔。奇怪的怪癖。无法说出为什么会发生这种情况,因为默认值应该意味着它始终被设置,但是如果您应用相关的架构更改/迁移
is_public = models.BooleanField(_('is public'), default , 这应该可以解决它=真,null=真)
Hmm. Odd quirk. Can't say why it is happening because the default should mean it's always set, but this should fix it, if you apply the relevant schema change/migration
is_public = models.BooleanField(_('is public'), default=True, null=True)