【问题标题】:sqlalchemy ORM call fails with TypeError: expected bytes, str foundsqlalchemy ORM 调用失败并出现 TypeError: expected bytes, str found
【发布时间】:2021-02-07 16:10:15
【问题描述】:

我正在用我编写的一些代码进行以下(相对)简单的调用:

pickups = session.query(Pickup).filter(Pickup.firebase_run_id == run_id).all()

我对此代码有两组集成测试,一组在本地运行并使用 psycops2 和 TLS,另一组在 GCP 开发环境(GCP 云功能)中运行并使用 pg8000 和 UNIX 套接字(GCP 要求 (?) 云函数通过 UNIX 套接字连接到 Cloud SQL - 请参阅 here)。本地集成测试运行良好。但是,开发测试可靠地失败并出现以下错误:

    Traceback (most recent call last):
      File "/user_code/main.py", line 245, in GET_run
        response = run_get(run_id)
      File "/user_code/rubbish_geo_client/ops.py", line 686, in run_get
        pickups = session.query(Pickup).filter(Pickup.firebase_run_id == run_id).all()
      File "/env/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3319, in all
        return list(self)
      File "/env/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3481, in __iter__
        return self._execute_and_instances(context)
      File "/env/local/lib/python3.7/site-packages/sqlalchemy/orm/query.py", line 3503, in _execute_and_instances
        querycontext, self._connection_from_session, close_with_result=True
    TypeError: expected bytes, str found

这个特定的函数调用实际上只是这种失败的一个例子;我所有的数据库调用都失败并出现相同的_execute_and_instances 错误!

也许有人对解决此问题的方法有所了解?

【问题讨论】:

    标签: sqlalchemy google-cloud-functions pg8000


    【解决方案1】:

    pg8000 在其实现中存在一个长期存在的错误,sqlalchemy 驱动程序刚刚修补了该错误。新的pg8000 维护者最近为这个问题引入了一个补丁,并发布了一个包含修复的新版本pg8000。不幸的是,这带来了破坏sqlalchemy 解决方法的副作用。

    在sqlalchemy 更改其驱动程序以尊重此补丁之前,目前最好的解决方案是将pg8000 从1.16.6 降级到1.16.5 或更低版本。

    您可以使用pip install pg8000<=1.16.5 这样做。

    参考以下对话:GH#commitcomment-43174891、GH#53。

    【讨论】:

      【解决方案2】:

      SQLAlchemy 1.4+ 中的 pg8000 is supported。

      【讨论】:

        【解决方案3】:
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-06
        • 1970-01-01
        • 2020-11-07
        • 2020-09-10
        • 2021-09-12
        • 2019-07-18
        相关资源
        最近更新 更多