的web2py:IntegrityError(“NOT NULL约束失败:post.message
问题描述:
我以为自己躲过/解决了这个错误,但现在坚持我db.py代码:。的web2py:IntegrityError(“NOT NULL约束失败:post.message
Post = db.define_table('post',
Field('message', 'text', requires=IS_NOT_EMPTY(), notnull=False),
Field('answers', 'text', requires=IS_NOT_EMPTY(), notnull=False),
auth.signature
)
Post.is_active.readable=False
Post.is_active.writeable=False
控制器:
@ auth.requires_login()
def index():
db.post.answers.writable=False
db.post.answers.readable=False
form = SQLFORM(post, formstyle='divs')
if form.process().accepted:
pass
messages = db(post).select(orderby=~post.created_on)
.......code
#after several codes in now need to post a message to answers field, WITHOUT using a form in the view page
db.post.insert(answers=report)
笔者认为:
{{for msg in messages:}}
code
{{=msg.message}}
{{report from answers field}}
我的问题是,我不断收到错误:IntegrityError(“NOT NULL约束失败:post.message
我怎么解决这个问题? 亲切的问候
答
如果数据库表最初是notnull=True
创建的,后来模式转变到notnull=False
不会有任何效果,去掉了NOT NULL
约束需要一个外部工具(DAL不能删除这样的限制)。
正如您在评论中所建议的,您可以改为将字段的默认值设置为类似于空字符串的内容,但如果确实需要允许空值,则应该使用数据库管理工具来删除来自数据库模式的约束。
你最初是否有'notnull = True',现在你不再需要这个限制了? – Anthony
是的,我确实拥有它 – wakamdr
http://www.mail-archive.com/[email protected]/msg12879.html .....这篇文章诀窍......默认='' – wakamdr