Search code examples
sqliteweb2py

Web2py: IntegrityError('NOT NULL constraint failed:post.message


I thought I had escaped/ solved this error. But now am stuck. My db.py code:

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

controller:

@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)

In my view:

{{for msg in messages:}}
code
{{=msg.message}}
{{report from answers field}}

My issue is that i keep getting the error: IntegrityError('NOT NULL constraint failed:post.message

How do I solve this error? Kind regards


Solution

  • If the database table was originally created with notnull=True, later changing the model to notnull=False will have no effect, as removing the NOT NULL constraint requires an external tool (the DAL cannot remove such constraints).

    As you suggest in your comment, you can instead set the default value for the field to something like an empty string, but if you do in fact want to allow null values, you should instead use a database administration tool to remove the constraint from the database schema.