Query returns different data when using MySQL and SQLite
When I query the following model with SQLAlchemy I don't get the expected result.
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True)
def __repr__(self):
return '<User %r>' % self.username
I created a MySQL table directly and then passed some data:
mysql> CREATE TABLE "users" (id INTEGER, username VARCHAR(64)); mysql> select * from users; +------+-------------+---------+ | id | username | role_id | +------+-------------+---------+ | NULL | yzxu | NULL | | NULL | after | NULL | +------+-------------+---------+
When using MySQL, SQLAlchemy query returns None
.
>>> User.query.filter_by(username='after').all()
[None]
The correct username is returned when using SQLite.
>>> db.create_all()
>>> db.session.add(User(username='after'))
>>> db.session.commit()
>>> User.query.filter_by(username='after').all()
[<User u'after'>]
+3
source to share