【问题标题】:SQLAlchemy raises an exception for unknown version while connecting to GaussDB (for openGauss)SQLAlchemy 在连接到 GaussDB 时引发未知版本的异常(对于 openGauss)
【发布时间】:2022-11-22 09:31:00
【问题描述】:

如下代码所示,我们可以选择一个ORM(Object-Relational Mapping)模块连接到GaussDB(针对openGauss)。我所知道的 Python 中最流行的第三方库是 SQLAlchemy。但是,当我通过以下代码连接到 openGauss 时,会引发已知版本的异常。

from sqlalchemy.engine import create_engine
from sqlalchemy.orm import sessionmaker

# ...
dsn = '{}://{}:{}@{}:{}/{}'.format(db_type, username, password, host, port, database)
engine = create_engine(dsn, pool_pre_ping=True)
session_maker = sessionmaker(bind=engine)

# Base is a base class for each table.
# We want to create tables' schema, but the exception raises.
Base.metadata.create_all(
            engine,
            checkfirst=check_first
        )

我们提到的例外是:

  File "C:\Users\wotchin\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\create.py", line 674, in first_connect
    dialect.initialize(c)
  File "C:\Users\wotchin\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 775, in initialize
    super(PGDialect_psycopg2, self).initialize(connection)
  File "C:\Users\wotchin\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\dialects\postgresql\base.py", line 3182, in initialize
    super(PGDialect, self).initialize(connection)
  File "C:\Users\wotchin\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\default.py", line 394, in initialize
    self.server_version_info = self._get_server_version_info(
  File "C:\Users\wotchin\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\dialects\postgresql\base.py", line 3435, in _get_server_version_info
    raise AssertionError(
AssertionError: Could not determine version from string '(GaussDB Kernel V500R002C00 build 434c09d8) compiled at 2021-06-26 10:18:58 commit 0 last mr 1692 debug on x86_64-unknown-linux-gnu, compiled by g++ (GCC) 7.3.0, 64-bit'

【问题讨论】:

    标签: python postgresql sqlalchemy


    【解决方案1】:

    我解决了这个问题。

    SQLAlchemy 有一个版本检查。因此,我们可以在使用 SQLAlchemy 之前修改函数操作。

            from sqlalchemy.dialects.postgresql.base import PGDialect
            PGDialect._get_server_version_info = lambda *args: (9, 2)
    

    【讨论】:

    • 我有类似的情况,我正在使用无法解析其版本的 postresql 变体。这是个好主意,覆盖了版本检查功能。不过,我想我们都必须非常小心地测试我们的代码!
    【解决方案2】:

    您现在可以尝试项目opengauss-sqlalchemy。

    openGauss最近提供了这个项目来支持SQLAlchemy。

    重写PGDialect的内部方法_get_server_version_info可以解决连接openGauss的问题,但是openGauss的一些SQL和PostgreSQL不一样。此外,openGauss 的保留字比PostgreSQL 多,除非我们将这些字用作表名或列名,否则我们可能不会注意到。

    该项目已发表于pypi , gitee 和github

    【讨论】:

    • 虽然这在理论上可以回答问题,it would be preferable 将答案的重要部分包含在此处,并提供链接以供参考。请edit提供所有相关信息的答案。
    • @Adriaan 感谢您的建议,我已经用相关信息更新了我的答案。
    • 感谢您的编辑。不过,还是没有多说。请edit回答解释为什么和如何这个项目将解决这个问题。
    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生变化,仅链接答案可能会失效。 - From Review
    猜你喜欢
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多