繁体   English   中英

Flask SQLAlchemy Heroku 和 Postgres 出错(psycopg2.errors.DatatypeMismatch)

[英]Flask SQLAlchemy Error with Heroku & Postgres (psycopg2.errors.DatatypeMismatch)

我已经用 Heroku 安装了我的 Python 应用程序,并且正在从我的 SQLite 数据库转移到 Postgres 数据库。 但是,我遇到了 SQLite 不会发生的以下错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DatatypeMismatch) argument of WHERE must be type boolean, not type character varying
LINE 3: WHERE site_clearance."CreatedOn"
              ^

[SQL: SELECT id 
FROM site_clearance 
WHERE site_clearance."CreatedOn"]
(Background on this error at: http://sqlalche.me/e/f405)

查询的相应 Python 代码用于生成 PDF 文件,正是此查询生成了错误:

# MAKE THE DB QUERIES HERE THAT ARE NEEDED TO BE ENTERED INTO EACH RECORD SHEET
for i in range(0, len(dictionary)):
    query = [r[0] for r in db.session.query(model).filter(model.CreatedOn).values(keys[i])]
    queries.append(query)

正如我所说,SQLite 不会发生此错误,并且问题始于 psycopg2 package。 下面的服务器日志中的堆栈跟踪。 任何帮助表示赞赏。 谢谢!

2020-08-10T17:54:25.630676+00:00 heroku[router]: at=info method=GET path="/makepdf_site_clearance" host=hidden-meadow-44024.herokuapp.com request_id=6bb8550d-afd0-415f-9e8b-bf0a86e63ab4 fwd="88.98.244.118" dyno=web.1 connect=1ms service=37ms status=500 bytes=484 protocol=https
2020-08-10T17:54:25.627181+00:00 app[web.1]: [2020-08-10 17:54:25,620] ERROR in app: Exception on /makepdf_site_clearance [GET]
2020-08-10T17:54:25.627193+00:00 app[web.1]: Traceback (most recent call last):
2020-08-10T17:54:25.627194+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
2020-08-10T17:54:25.627194+00:00 app[web.1]: cursor, statement, parameters, context
2020-08-10T17:54:25.627195+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
2020-08-10T17:54:25.627196+00:00 app[web.1]: cursor.execute(statement, parameters)
2020-08-10T17:54:25.627196+00:00 app[web.1]: psycopg2.errors.DatatypeMismatch: argument of WHERE must be type boolean, not type character varying
2020-08-10T17:54:25.627196+00:00 app[web.1]: LINE 3: WHERE site_clearance."CreatedOn"
2020-08-10T17:54:25.627197+00:00 app[web.1]: ^
2020-08-10T17:54:25.627198+00:00 app[web.1]:
2020-08-10T17:54:25.627198+00:00 app[web.1]:
2020-08-10T17:54:25.627199+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2020-08-10T17:54:25.627199+00:00 app[web.1]:
2020-08-10T17:54:25.627200+00:00 app[web.1]: Traceback (most recent call last):
2020-08-10T17:54:25.627200+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
2020-08-10T17:54:25.627201+00:00 app[web.1]: response = self.full_dispatch_request()
2020-08-10T17:54:25.627201+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
2020-08-10T17:54:25.627202+00:00 app[web.1]: rv = self.handle_user_exception(e)
2020-08-10T17:54:25.627202+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
2020-08-10T17:54:25.627203+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2020-08-10T17:54:25.627203+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
2020-08-10T17:54:25.627203+00:00 app[web.1]: raise value
2020-08-10T17:54:25.627204+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
2020-08-10T17:54:25.627204+00:00 app[web.1]: rv = self.dispatch_request()
2020-08-10T17:54:25.627205+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
2020-08-10T17:54:25.627205+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2020-08-10T17:54:25.627206+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask_login/utils.py", line 272, in decorated_view
2020-08-10T17:54:25.627206+00:00 app[web.1]: return func(*args, **kwargs)
2020-08-10T17:54:25.627207+00:00 app[web.1]: File "/app/constructaerp02/routes.py", line 1191, in makepdf_site_clearance
2020-08-10T17:54:25.627207+00:00 app[web.1]: p = pdf_make(model=SiteClearance, dictionary=SiteClearance_dictionary, title='Site Clearance')
2020-08-10T17:54:25.627208+00:00 app[web.1]: File "/app/constructaerp02/pdf_creator.py", line 23, in pdf_make
2020-08-10T17:54:25.627208+00:00 app[web.1]: query = [r[0] for r in db.session.query(model).filter(model.CreatedOn).values(keys[i])]
2020-08-10T17:54:25.627209+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 1530, in values
2020-08-10T17:54:25.627209+00:00 app[web.1]: return iter(q)
2020-08-10T17:54:25.627210+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3481, in __iter__
2020-08-10T17:54:25.627210+00:00 app[web.1]: return self._execute_and_instances(context)
2020-08-10T17:54:25.627211+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3506, in _execute_and_instances
2020-08-10T17:54:25.627211+00:00 app[web.1]: result = conn.execute(querycontext.statement, self._params)
2020-08-10T17:54:25.627212+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1020, in execute
2020-08-10T17:54:25.627212+00:00 app[web.1]: return meth(self, multiparams, params)
2020-08-10T17:54:25.627212+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
2020-08-10T17:54:25.627213+00:00 app[web.1]: return connection._execute_clauseelement(self, multiparams, params)
2020-08-10T17:54:25.627213+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_clauseelement
2020-08-10T17:54:25.627214+00:00 app[web.1]: distilled_params,
2020-08-10T17:54:25.627215+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1324, in _execute_context
2020-08-10T17:54:25.627215+00:00 app[web.1]: e, statement, parameters, cursor, context
2020-08-10T17:54:25.627216+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1518, in _handle_dbapi_exception
2020-08-10T17:54:25.627216+00:00 app[web.1]: sqlalchemy_exception, with_traceback=exc_info[2], from_=e
2020-08-10T17:54:25.627216+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
2020-08-10T17:54:25.627217+00:00 app[web.1]: raise exception
2020-08-10T17:54:25.627217+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
2020-08-10T17:54:25.627218+00:00 app[web.1]: cursor, statement, parameters, context
2020-08-10T17:54:25.627218+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
2020-08-10T17:54:25.627219+00:00 app[web.1]: cursor.execute(statement, parameters)
2020-08-10T17:54:25.627223+00:00 app[web.1]: sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DatatypeMismatch) argument of WHERE must be type boolean, not type character varying
2020-08-10T17:54:25.627224+00:00 app[web.1]: LINE 3: WHERE site_clearance."CreatedOn"
2020-08-10T17:54:25.627224+00:00 app[web.1]: ^
2020-08-10T17:54:25.627224+00:00 app[web.1]:
2020-08-10T17:54:25.627225+00:00 app[web.1]: [SQL: SELECT id
2020-08-10T17:54:25.627225+00:00 app[web.1]: FROM site_clearance
2020-08-10T17:54:25.627226+00:00 app[web.1]: WHERE site_clearance."CreatedOn"]
2020-08-10T17:54:25.627233+00:00 app[web.1]: (Background on this error at: http://sqlalche.me/e/f405)
2020-08-10T17:54:25.631141+00:00 app[web.1]: 10.63.174.10 - - [10/Aug/2020:17:54:25 +0000] "GET 

该错误准确地说明了它正在发生的事情; 您正在使用.filter(model.createdOn) ,但createdOn是什么?

一种选择是.filter(model.createdOn > datetime.now() - timedelta(days=7))会起作用,因为它会生成 SQL "createdOn" > "2020-08-03 12:00:00"或类似的东西.

另一种选择是.filter(model.createdOn.isnot(None)).filter(~model.createdOn.is_(None)) ,它们都生成"createdOn" IS NOT NULL

在这种情况下,打印查询通常是个好主意,为变量输入一些虚拟值,如果直接执行 SQL,看看它是否运行。 这对调试有很大帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM