最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【无需解决】Flask中SQLAlchemy中使用Pagination的prev出错:_mysql_exceptions ProgrammingError 1064 You have an error in your SQL syntax

Flask crifan 827浏览 0评论

flask中代码:

        curPageTaskList = None
        # for debug
        taskPagination = None
        if curRole == UserRole.Initiator:
            taskPagination = Task.query.filter_by(initiatorId=userId).paginate(
                page=curPageNum,
                per_page=numberPerPage,
                error_out=False)
        elif curRole == UserRole.Errandor:
            taskPagination = Task.query.filter_by(errandorId=userId).paginate(
                page=curPageNum,
                per_page=numberPerPage,
                error_out=False)
        gLog.debug(“type(taskPagination)=%s”
                   “,taskPagination=%s,”
                   “,has_next=%s”
                   “,has_prev=%s”,
                   “,items=%s”,
                   “,next()=%s”,
                   “,next_num=%s”,
                   “,page=%s”,
                   “,pages=%s”,
                   “,per_page=%s”,
                   “,prev()=%s”,
                   “,prev_num=%s”,
                   “,query=%s”,
                   “,total=%s”,
                   type(taskPagination),
                   taskPagination,
                   taskPagination.has_next,
                   taskPagination.has_prev,
                   taskPagination.items,
                   taskPagination.next(error_out=False),
                   taskPagination.next_num,
                   taskPagination.page,
                   taskPagination.pages,
                   taskPagination.per_page,
                   taskPagination.prev(error_out=False),
                   taskPagination.prev_num,
                   taskPagination.query,
                   taskPagination.total
                   )

结果出错:

Traceback (most recent call last):
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/gevent/pywsgi.py”, line 884, in handle_one_response
    self.run_application()
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/geventwebsocket/handler.py”, line 88, in run_application
    return super(WebSocketHandler, self).run_application()
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/gevent/pywsgi.py”, line 870, in run_application
    self.result = self.application(self.environ, self.start_response)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_sockets.py”, line 48, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_restful/__init__.py”, line 271, in error_router
    return original_handler(e)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_restful/__init__.py”, line 268, in error_router
    return self.handle_error(e)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_restful/__init__.py”, line 271, in error_router
    return original_handler(e)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_restful/__init__.py”, line 268, in error_router
    return self.handle_error(e)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_restful/__init__.py”, line 477, in wrapper
    resp = resource(*args, **kwargs)
  File “/root/RunningFast/staging/runningfast/resources/Accesstoken.py”, line 167, in decorated_function
    return f(*args, **kwargs)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask/views.py”, line 84, in view
    return self.dispatch_request(*args, **kwargs)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_restful/__init__.py”, line 587, in dispatch_request
    resp = meth(*args, **kwargs)
  File “/root/RunningFast/staging/runningfast/resources/User.py”, line 540, in get
    taskPagination.prev(error_out=False),
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 342, in prev
    return self.query.paginate(self.page – 1, self.per_page, error_out)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 476, in paginate
    items = self.limit(per_page).offset((page – 1) * per_page).all()
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/orm/query.py”, line 2643, in all
    return list(self)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/orm/query.py”, line 2795, in __iter__
    return self._execute_and_instances(context)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/orm/query.py”, line 2818, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 945, in execute
    return meth(self, multiparams, params)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/sql/elements.py”, line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1189, in _execute_context
    context)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1393, in _handle_dbapi_exception
    exc_info
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/util/compat.py”, line 202, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1182, in _execute_context
    context)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/sqlalchemy/engine/default.py”, line 462, in do_execute
    cursor.execute(statement, parameters)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/MySQLdb/cursors.py”, line 205, in execute
    self.errorhandler(self, exc, value)
  File “/root/Envs/RunningFast/lib/python2.7/site-packages/MySQLdb/connections.py”, line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (_mysql_exceptions.ProgrammingError) (1064, “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 ‘-10, 10′ at line 4”) [SQL: u’SELECT tasks.id AS tasks_id, tasks.`statusType` AS `tasks_statusType`, tasks.`itemType` AS `tasks_itemType`, tasks.`promotionCode` AS `tasks_promotionCode`, tasks.`hasEnded` AS `tasks_hasEnded`, tasks.`endedTime` AS `tasks_endedTime`, tasks.`createdAt` AS `tasks_createdAt`, tasks.`updatedAt` AS `tasks_updatedAt`, tasks.`initiatorId` AS `tasks_initiatorId`, tasks.`errandorId` AS `tasks_errandorId`, tasks.`cancelByUserType` AS `tasks_cancelByUserType`, tasks.`initiatorTipType` AS `tasks_initiatorTipType`, tasks.`initiatorStartLocationId` AS `tasks_initiatorStartLocationId`, tasks.`initiatorEndLocationId` AS `tasks_initiatorEndLocationId`, tasks.`initiatorDescription` AS `tasks_initiatorDescription`, tasks.`initiatorCancelReasonType` AS `tasks_initiatorCancelReasonType`, tasks.`errandorStartLocationId` AS `tasks_errandorStartLocationId`, tasks.`errandorEndLocationId` AS `tasks_errandorEndLocationId`, tasks.`errandorDescription` AS `tasks_errandorDescription`, tasks.`errandorCancelReasonType` AS `tasks_errandorCancelReasonType`, tasks.`errandorStartTime` AS `tasks_errandorStartTime`, tasks.`errandorRatingType` AS `tasks_errandorRatingType` \nFROM tasks \nWHERE tasks.`initiatorId` = %s \n LIMIT %s, %s’] [parameters: (‘user-bb22f24e-3c27-4e7b-867a-b855e139b295’, -10, 10)]
{‘GATEWAY_INTERFACE’: ‘CGI/1.1’,
‘HTTP_ACCEPT’: ‘*/*’,
‘HTTP_ACCEPT_ENCODING’: ‘gzip, deflate, sdch’,
‘HTTP_ACCEPT_LANGUAGE’: ‘zh-CN,zh;q=0.8,en;q=0.6’,
‘HTTP_AUTHORIZATION’: ‘HmucMnNvaDtTOk21LuvP1FLGBiznisLI’,
‘HTTP_CACHE_CONTROL’: ‘no-cache’,
‘HTTP_CONNECTION’: ‘keep-alive’,
‘HTTP_HOST’: ‘115.29.173.126:21085’,
‘HTTP_POSTMAN_TOKEN’: ‘e0cdf176-230e-7ad2-1eb5-953546a3f539’,
‘HTTP_USER_AGENT’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36’,
‘PATH_INFO’: ‘/runningfast/api/v1.0/users/user-bb22f24e-3c27-4e7b-867a-b855e139b295/tasks’,
‘QUERY_STRING’: ‘curRole=Initiator’,
‘REMOTE_ADDR’: ‘58.209.20.165’,
‘REMOTE_PORT’: ‘10070’,
‘REQUEST_METHOD’: ‘GET’,
‘SCRIPT_NAME’: ”,
‘SERVER_NAME’: ‘AY140128113754462e2eZ’,
‘SERVER_PORT’: ‘21085’,
‘SERVER_PROTOCOL’: ‘HTTP/1.1’,
‘SERVER_SOFTWARE’: ‘gevent/1.1.2 gunicorn/19.6.0’,
‘werkzeug.request’: <Request ‘http://115.29.173.126:21085/runningfast/api/v1.0/users/user-bb22f24e-3c27-4e7b-867a-b855e139b295/tasks?curRole=Initiator’ [GET]>,
‘wsgi.errors’: <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f82b0eb4f50>,
‘wsgi.file_wrapper’: <class ‘gunicorn.http.wsgi.FileWrapper’>,
‘wsgi.input’: <gevent.pywsgi.Input object at 0x7f82b0ecc188>,
‘wsgi.multiprocess’: False,
‘wsgi.multithread’: True,
‘wsgi.run_once’: False,
‘wsgi.url_scheme’: ‘http’,
‘wsgi.version’: (1, 0)} failed with ProgrammingError

很明显是:

taskPagination.prev(error_out=False)

出错了

-》其中,我已经把之前的:

taskPagination = Task.query.filter_by(initiatorId=userId).paginate(
                page=curPageNum,
                per_page=numberPerPage,
                error_out=False)

中的error_out从之前的True改为False了

以及:

taskPagination.prev(error_out=False)

-》希望后续访问该对象时时不要报错

-》但是还是报错

-》看来是:

内部的sqlalchemy出错了?

至少是:

此处的报错,应该忽略掉,而返回对应的None对象才对。

flask sqlalchemy pagination prev ProgrammingError

[Flask] SQLAlchemy + pagination + MySQL problems

SQLAlchemy pagination + MySQL – Stack Overflow

->

看来是SQLAlchemy自己的bug

暂时懒得理会了。

暂时不去打印输出prev了

-》以后如果需要用到的话,需要自己注意一下:

当curPageNum==1时,注意不要调用paginationObj.prev()

否则会报此错误的。。。

即使你error_out为False:

paginationObj.prev(error_out=False)

sqlalchemy pagination prev ProgrammingError 1064

sqlalchemy pagination prev 1064

python – Flask SQLAlchemy pagination error – Stack Overflow

转载请注明:在路上 » 【无需解决】Flask中SQLAlchemy中使用Pagination的prev出错:_mysql_exceptions ProgrammingError 1064 You have an error in your SQL syntax

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
62 queries in 0.163 seconds, using 19.00MB memory