PythonAnywhere SQLite/SQLAlchemy 突然出现错误

PythonAnywhere SQLite/SQLAlchemy suddenly getting errors

SQLAlchemy 和 SQLite 错误出现在 PythonAnywhere 上,但在本地主机上没有问题。 所以我刚刚将我的 Flask 站点上传到 pythonanywhere,当我尝试访问它时它无法访问我的数据库,而是出现了一堆错误。

2021-01-30 10:42:05,740: Exception on /m/550 [GET]
 Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
self.dialect.do_execute(
 File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 580, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: near "(": syntax error
**NO MATCH**

我在本地计算机上没有收到这些异常。稍微谷歌一下,这似乎是因为 PA 使用了旧版本的 SQLite?哪个开始看起来有点愚蠢,但我似乎无法更新它?

所以我想知道是否有一些聪明的解决方案,或者切换到 MySQL 是否更容易?这样做会不会有其他问题?或者我是否只需要切换 SQLALCHEMY_DATABASE_URI 以适应 MySQL?我现在的样子是这样的:

SQLALCHEMY_DATABASE_URI = 'sqlite:////home/movieelo/cinelow/my_server/database/database.db'
SQLALCHEMY_TRACK_MODIFICATIONS = False

我们将不胜感激所有的帮助和指导!

更新: 我设法切换到 MySQL,但仍然收到 SQL 语法相关错误,例如:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in 
 your SQL syntax; check the manual that corresponds to your MySQL server 
 version for the right syntax to use near '(PARTITION BY 
 movie_category_scores.category_id ORDER BY movie_category_scores.s' at line 2
 **NO MATCH**

更新 2

我想我现在已经确定了导致错误的单个函数,它看起来像这样:

def get_top_movies_by_category(category_id):
    query = db.session.query(
    MovieCategoryScores,
    func.rank()\
        .over(
            order_by=MovieCategoryScores.score.desc(),
            partition_by=MovieCategoryScores.category_id,
        )\
        .label('rank')
    ).filter(MovieCategoryScores.votes >= 10)
    # now filter
    query = query.filter(MovieCategoryScores.category_id == category_id)
    query = query.order_by(MovieCategoryScores.category_id, 'rank')
    movies = query.all()
    return movies

产生此错误的原因:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    self.dialect.do_execute(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 580, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: near "(": syntax error

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/movieelo/cinelow/my_server/database/pers_movie_dbf.py", line 137, in get_top_movies_by_category
    movies = query.all()
  File "/usr/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3186, in all
    return list(self)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3342, in __iter__
    return self._execute_and_instances(context)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 3367, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 988, in execute
    return meth(self, multiparams, params)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1101, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1252, in _execute_context
    self._handle_dbapi_exception(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1473, in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 152, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1248, in _execute_context
    self.dialect.do_execute(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 580, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) near "(": syntax error
[SQL: SELECT movie_category_scores.votes >= ? AS anon_1, movie_category_scores.movie_id AS movie_category_scores_movie_id, movie_category_scores.category_id AS movie_category_scores_category_id, movie_category_scores.score AS movie_category_scores_score, movie_category_scores.votes AS movie_category_scores_votes, rank() OVER (PARTITION BY movie_category_scores.category_id ORDER BY movie_category_scores.score DESC) AS rank 
FROM movie_category_scores 
WHERE movie_category_scores.votes >= ? AND movie_category_scores.category_id = ? ORDER BY movie_category_scores.category_id, rank]
[parameters: (10, 10, 11)]
(Background on this error at: http://sqlalche.me/e/e3q8)

贾尔斯·托马斯(Python任何地方)confirmed that the SQLite version available on PythonAnywhere doesn't support window functions

Window 功能已添加 to SQLite version 3.25.0。以后,您可以通过 运行:

检查 Python 安装中包含的 SQLite 版本是否支持 windows 功能
import sqlite3

if sqlite3.sqlite_version_info > (3, 25, 0):
    print("Window functions are supported!")
else:
    print("Window functions are not supported :(")