Forums

Flask + Sql Alchemy Trouble... Works fine on localhost

Hi everyone,

I'm having some troubles moving my site over to PythonAnywhere, every time I try to load my webapp I just get internal server errors and nothing works.

It seems to be related to my database queries.

The only query being run on the homepage is the following function. I'm using SqlAlchemy and SQLite.

def get_top_ten():
engine = connect_db()
query = select(wrestlers).order_by(wrestlers.c.elo.desc()).limit(10)
result = engine.execute(query)
return result.fetchall()

I've tested that connect_db() is not throwing the error, its only when I actually attempt to query.

Below is the error log I received. Any help would be greatly appreciated.

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/selectable.py", line 3102, in __init__
    cols_present = bool(columns)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 535, in __bool__
    raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined
**NO MATCH**
The above exception was the direct cause of the following exception:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1485, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/zacharydangelo/kayfabelo/flask_app.py", line 17, in home
    top_ten = convert_to_list(get_top_ten())
  File "/home/zacharydangelo/kayfabelo/DB/model.py", line 332, in get_top_ten
    query = select(wrestlers).order_by(wrestlers.c.elo.desc()).limit(10)
  File "<string>", line 2, in select
  File "<string>", line 2, in __init__
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/selectable.py", line 3104, in __init__
    util.raise_(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python list or other iterable
2022-04-11 23:49:45,398: Exception on /favicon.ico [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1475, in dispatch_request
    self.raise_routing_exception(req)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1457, in raise_routing_exception
    raise request.routing_exception  # type: ignore
  File "/usr/local/lib/python3.9/site-packages/flask/ctx.py", line 349, in match_request
    result = self.url_adapter.match(return_rule=True)  # type: ignore
  File "/usr/local/lib/python3.9/site-packages/werkzeug/routing.py", line 2032, in match
    raise NotFound()
werkzeug.exceptions.NotFound: 404 Not Found: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
**NO MATCH**
During handling of the above exception, another exception occurred:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/selectable.py", line 3102, in __init__
    cols_present = bool(columns)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 535, in __bool__
    raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined
**NO MATCH**
The above exception was the direct cause of the following exception:
**NO MATCH**
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2051, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1501, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1369, in handle_user_exception
    return self.handle_http_exception(e)
  File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1309, in handle_http_exception
    return self.ensure_sync(handler)(e)
  File "/home/zacharydangelo/kayfabelo/flask_app.py", line 12, in not_found
    return home()
  File "/home/zacharydangelo/kayfabelo/flask_app.py", line 17, in home
    top_ten = convert_to_list(get_top_ten())
  File "/home/zacharydangelo/kayfabelo/DB/model.py", line 332, in get_top_ten
    query = select(wrestlers).order_by(wrestlers.c.elo.desc()).limit(10)
  File "<string>", line 2, in select
  File "<string>", line 2, in __init__
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py", line 139, in warned
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/selectable.py", line 3104, in __init__
    util.raise_(
  File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
    raise exception
sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python list or other iterable

As the error message says: "columns argument to select() must be a Python list or other iterable", make sure that wrestlers is an object of correct type.

As the error message says: "columns argument to select() must be a Python list or other iterable", make sure that wrestlers is an object of correct type.

I guess I'm just confused about why that exact line would work on my machine but not on PythonAnywhere?

I know wrestlers is the name of a table, I'm using the same DB file I was on my machine and the connection seems to be working just fine

Edit: I guess I'm realizing, this is due to PythonAnywhere using SQLAlchemy 1.3, while on my machine I am using 1.4

Is it at all possible to upgrade my instance of PythonAnywhere to 1.4?

If you do not use virtualenv, pip install new version with --user flag. Use relevant pip for you python version so pip3.9 if you run with python3.9 etc.

Thanks, I was able to update sql alchemy and now I'm no longer getting errors.

Glad to hear that!