Flask-SQLAlchemy TypeError: %d format: a number is required, not str when using filter on a string field

  flask, flask-sqlalchemy, mariadb, mysql, python

I really don’t have a clue of what is going on that i can’t filter this query, tried filter_by() and filter() both return the same error, it is probably a stupid mistake that i’m failing to notice, your help would be really appreciated.
The MariaDB(MySQL) table structure is

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| classe       | int(11)      | NO   |     | NULL    |                |
| nome         | varchar(20)  | NO   |     | NULL    |                |
| sobrenome    | varchar(50)  | NO   |     | NULL    |                |
| razao_social | varchar(255) | YES  |     | NULL    |                |
| cpf_cnpj     | varchar(18)  | NO   | UNI | NULL    |                |
| rg_ie        | varchar(15)  | NO   | UNI | NULL    |                |
| email        | varchar(120) | NO   | UNI | NULL    |                |
| fone         | varchar(11)  | YES  |     | NULL    |                |
| celular      | varchar(14)  | NO   | UNI | NULL    |                |
| password     | varchar(60)  | NO   |     | NULL    |                |
| date_created | datetime     | NO   |     | NULL    |                |
| ip_created   | varchar(45)  | YES  |     | NULL    |                |
| api_token    | varchar(60)  | NO   |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

The exception is

TypeError: %d format: a number is required, not str

when using filter on a string field
on line

user = User.query.filter(User.api_token == token).first()

Here is the whole route function

@app.route('/api/login', methods=['GET','POST'])
def api_login():
    if current_user.is_authenticated:
        return "3"
    token = request.args.get('apitoken', '1', type=String)
    ip_ban.add(ip=request.host.split(':')[0],url=url_for('login'))
    user = User.query.filter(User.api_token == token).first()
    if user:
        if current_user.classe == -1 :
            return "-1"
        if current_user.classe == 0 :
            return "0"
        login_user(user)
        loginrec = UserLogins(user_id= user.id, login_ip= request.host.split(':')[0])
        db.session.add(loginrec)
        db.session.commit()
        return "2"
    else:
        return token

Source: Python Questions

LEAVE A COMMENT