Peewee ORM gives IntegrityError: user_id may not be NULL
Question: I'm trying to use the Peewee ORM for my new (Flask) website, and now I ran
into a problem. I just created a simple model like so:

    
    
    from peewee import TextField, DateTimeField, IntegerField, ForeignKeyField
    from app import db
    
    ROLE_USER = 0
    ROLE_ADMIN = 1
    
    class User(db.Model):
        nickname = TextField()
        email = TextField()
        role = IntegerField(default = ROLE_USER)
    
    class Post(db.Model):
        body = TextField()
        timestamp = DateTimeField()
        user = ForeignKeyField(User, related_name='posts')
    

So I created two users, after which I wanted to create a new post. I did this
like so:

    
    
    >>> from app.models import User
    >>> u = User(nickname='john', email='john@email.com', role=0)
    >>> u.save()
    >>> from app.models import Post
    >>> from datetime import datetime as dt
    >>> p = Post(body='FIPO!!', timestamp = dt.now(), author=u)
    >>> p.save()
    No handlers could be found for logger "peewee"
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/peewee.py", line 2479, in save
        ret_pk = self.insert(**field_dict).execute()
      File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/peewee.py", line 1775, in execute
        return self.database.last_insert_id(self._execute(), self.model_class)
      File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/peewee.py", line 1470, in _execute
        return self.database.execute_sql(sql, params, self.require_commit)
      File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/peewee.py", line 1885, in execute_sql
        return self.sql_error_handler(exc, sql, params, require_commit)
      File "/Users/kramer65/dev/repos/microblog/flask/lib/python2.7/site-packages/peewee.py", line 1871, in sql_error_handler
        raise exception
    sqlite3.IntegrityError: post.user_id may not be NULL
    

I don't understand that user_id apparently is NULL, because as far as I
understand, peewee should take care of this right?

Am I doing something wrong (and if so; what?), or is this a problem with
peewee? All tips are welcome!


Answer: The problem is you're using `author=` when you should be using `user=`.

Original

    
    
    p = Post(body='FIPO!!', timestamp = dt.now(), author=u)
    

Fixed

    
    
    p = Post(body='FIPO!!', timestamp = dt.now(), user=u)
    

