Search code examples
pythonpostgresqlherokuheroku-postgres

Heroku postgresql operator does not exist


I deployed my site to Heroku running postgresql. Before, I had it on the flask development environment running sqlite. The app ran fine when using the schedule view, but when I access the schedule view from Heroku, I get an error.

CLASS

class Task(db.Model):

id = db.Column(db.Integer, primary_key=True)

name = db.Column(db.String(80), index=True)

description = db.Column(db.String(200), index=True)

priority = db.Column(db.Integer)

is_complete = db.Column(db.Boolean)  ####might be trouble

url = db.Column(db.String(200), index=True)

est_dur = db.Column(db.Integer)
time_quad = db.Column(db.Integer)

timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)

user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

ROUTE

@bp.route('/schedule')
#@login_required
def schedule():


currentUser = current_user.id

q1 = Task.query.filter_by(time_quad=1).filter_by(user_id=currentUser).filter_by(is_complete=0).all()

q2 = Task.query.filter_by(time_quad=2).filter_by(user_id=currentUser).filter_by(is_complete=0).all()

q3 = Task.query.filter_by(time_quad=3).filter_by(user_id=currentUser).filter_by(is_complete=0).all()

q4 = Task.query.filter_by(time_quad=4).filter_by(user_id=currentUser).filter_by(is_complete=0).all()

taskAll = q1 + q2 + q3 + q4


print("current user" + str(currentUser))


return render_template('schedule.html', taskList = taskAll)

ERROR

Exception on /schedule [GET]
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
    context)
  File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 509, in do_execute
    cursor.execute(statement, parameters)
psycopg2.ProgrammingError: operator does not exist: boolean = integer
LINE 3: ....time_quad = 1 AND task.user_id = 1 AND task.is_complete = 0


HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
 [SQL: 'SELECT task.id AS task_id, task.name AS task_name, task.description AS task_description, task.priority AS task_priority, task.is_complete AS task_is_complete, task.url AS task_url, task.est_dur AS task_est_dur, task.time_quad AS task_time_quad, task.timestamp AS task_timestamp, task.user_id AS task_user_id \nFROM task \nWHERE task.time_quad = %(time_quad_1)s AND task.user_id = %(user_id_1)s AND task.is_complete = %(is_complete_1)s'] [parameters: {'time_quad_1': 1, 'user_id_1': 1, 'is_complete_1': 0}] (Background on this error at: http://sqlalche.me/e/f405)

Solution

  • PostgreSQL has a real boolean type but SQLite doesn't, SQLite generally uses one and zero for true and false (respectively). When you say this:

    filter_by(is_complete=0)
    

    the 0 will be interpreted by SQLite as "false" but by PostgreSQL as just the number zero; hence the complaint at Heroku about not being able to compare a boolean and an integer in task.is_complete = 0. If you mean "false", say so:

    filter_by(is_complete=False)
    

    That should be converted to zero when talking to SQLite and the proper boolean 'f' (or false) when talking to PostgreSQL.

    Once you fix that, I strongly recommend that you install PostgreSQL in your development environment if that's the database you're going to be deploying on. You'll have a much better time of things if you develop, test, and deploy on the same stack.