vendredi 27 novembre 2015

Flask-SQLAlchemy SQLITE boolean field always returns False

I'm using sqlite with SQLAlchemy. I have a class (trimmed) defined like so:

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    can_view_records = db.Column(db.Boolean, default=False, nullable=False)

With my schema defined like so:

create TABLE users (
    id INTEGER not null,
    can_view_records BOOLEAN not null,
    PRIMARY KEY (id)
);

In my database, I have the following rows:

+----+------------------+
| id | can_view_records |
+----+------------------+
| 8  | 1                |
| 9  | 0                |
+----+------------------+

Querying them in Python always gives me False for the can_view_records field.

>>> User.query.get(8).can_view_records
False
>>> User.query.get(9).can_view_records
False

But performing a filter on that field succeeds:

>>> User.query.filter_by(can_view_records=True).all()
[<id 8>]

but even that result has returns False:

>>> User.query.filter_by(can_view_records=True).first().can_view_records
False

Am I missing something?

Aucun commentaire:

Enregistrer un commentaire