SQLAlchemy raises an exception for unknown version while connecting to GaussDB (for openGauss)

Viewed 181

As the following code shows, we may choose an ORM (Object-Relational Mapping) module to connect to GaussDB (for openGauss). The most popular third-party library in Python I know is SQLAlchemy. But while I connect to the openGauss through the following code, an exception for known version raises.

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
        )

The exception we mentioned is:

  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'
1 Answers

I solved the problem.

SQLAlchemy has a version check. Hence, we can modify the function action before using SQLAlchemy.

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