Search code examples
pythonflaskpeeweeflask-login

Flask login get_id method gives error with Peewee


I'm trying to use peewee in combination with Flask-login, and I now get into trouble when implementing the get_id() method. Following the example as given by this tutorial, the User class I have is as follows:

class User(db.Model, BaseUser):
    username = CharField()
    password = CharField()

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

When trying to save a user to the (sqlite) DB, I get the following error:

>>> from app.models import User
>>> u = User()
>>> u.username = 'lala'
>>> u.password = 'blabla'
>>> u.save()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/peewee.py", line 3488, in save
    rows = self.update(**field_dict).where(self.pk_expr()).execute()
  File "/Library/Python/2.7/site-packages/peewee.py", line 2487, in execute
    return self.database.rows_affected(self._execute())
  File "/Library/Python/2.7/site-packages/peewee.py", line 2119, in _execute
    sql, params = self.sql()
  File "/Library/Python/2.7/site-packages/peewee.py", line 2484, in sql
    return self.compiler().generate_update(self)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1482, in generate_update
    return self.build_query(clauses, alias_map)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1357, in build_query
    return self.parse_node(Clause(*clauses), alias_map)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1318, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1293, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1246, in _parse_clause
    node.nodes, alias_map, conv, node.glue)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1335, in parse_node_list
    node_sql, node_params = self.parse_node(node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1318, in parse_node
    sql, params, unknown = self._parse(node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1293, in _parse
    sql, params = self._parse_map[node_type](node, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1227, in _parse_expression
    rhs, rparams = self.parse_node(node.rhs, alias_map, conv)
  File "/Library/Python/2.7/site-packages/peewee.py", line 1320, in parse_node
    params = [conv.db_value(i) for i in params]
  File "/Library/Python/2.7/site-packages/peewee.py", line 674, in db_value
    return value if value is None else self.coerce(value)
ValueError: invalid literal for int() with base 10: 'None'

If I remove the get_id() method I get no error, so that method seems to be the source of all evil. Would anybody know whats wrong with it? All tips are welcome!


Solution

  • Peewee models implement a get_id() method, which is used by the pk_expr() method. I am guessing that is the source of the issue.

    Fixed in master: https://github.com/coleifer/peewee/issues/435