Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: MetaData.reflect() raises exception with sqlalchemy v2.0.36 #1128

Closed
1 task done
daniel-thom opened this issue Oct 19, 2024 · 4 comments · Fixed by #1147
Closed
1 task done

[Bug]: MetaData.reflect() raises exception with sqlalchemy v2.0.36 #1128

daniel-thom opened this issue Oct 19, 2024 · 4 comments · Fixed by #1147
Assignees
Labels
bug Something isn't working

Comments

@daniel-thom
Copy link

What happened?

This commit in sqlalchemy v2.0.36 causes a regression when creating a table in DuckDB. A SQLAlchemy developer says that this package will need to override the new statement.

from sqlalchemy import MetaData, create_engine, text

engine = create_engine("duckdb:///:memory:")
metadata = MetaData()
with engine.connect() as conn:
    conn.execute(text("CREATE TABLE tbl(col1 INTEGER)"))
    conn.commit()
metadata.reflect(engine)
/private/tmp/env/lib/python3.11/site-packages/duckdb_engine/__init__.py:174: DuckDBEngineWarning: duckdb-engine doesn't yet support reflection on indices
  warnings.warn(
Traceback (most recent call last):
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
    self.dialect.do_execute(
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 941, in do_execute
    cursor.execute(statement, parameters)
  File "/private/tmp/env/lib/python3.11/site-packages/duckdb_engine/__init__.py", line 140, in execute
    self.__c.execute(statement, parameters)
duckdb.duckdb.CatalogException: Catalog Error: Type with name REGCLASS does not exist!

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/dthom/repos/chronify/scripts/repro_alchemy.py", line 8, in <module>
    metadata.reflect(engine)
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/sql/schema.py", line 5828, in reflect
    _reflect_info = insp._get_reflection_info(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/reflection.py", line 2024, in _get_reflection_info
    table_comment=run(self.get_multi_table_comment, optional=True),
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/reflection.py", line 2001, in run
    res = meth(filter_names=_fn, **kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/reflection.py", line 1377, in get_multi_table_comment
    self.dialect.get_multi_table_comment(
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/dialects/postgresql/base.py", line 4709, in get_multi_table_comment
    result = connection.execute(query, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
    return meth(
           ^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/sql/elements.py", line 515, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1640, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
    return self._exec_single_context(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1986, in _exec_single_context
    self._handle_dbapi_exception(
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2355, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
    self.dialect.do_execute(
  File "/private/tmp/env/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 941, in do_execute
    cursor.execute(statement, parameters)
  File "/private/tmp/env/lib/python3.11/site-packages/duckdb_engine/__init__.py", line 140, in execute
    self.__c.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (duckdb.duckdb.CatalogException) Catalog Error: Type with name REGCLASS does not exist!
[SQL: SELECT pg_catalog.pg_class.relname, pg_catalog.pg_description.description 
FROM pg_catalog.pg_class LEFT OUTER JOIN pg_catalog.pg_description ON pg_catalog.pg_class.oid = pg_catalog.pg_description.objoid AND pg_catalog.pg_description.objsubid = $1 AND pg_catalog.pg_description.classoid = CAST($2 AS REGCLASS) JOIN pg_catalog.pg_namespace ON pg_catalog.pg_namespace.oid = pg_catalog.pg_class.relnamespace 
WHERE pg_catalog.pg_class.relkind = ANY (ARRAY[$3, $4, $5]) AND pg_catalog.pg_table_is_visible(pg_catalog.pg_class.oid) AND pg_catalog.pg_namespace.nspname != $6 AND pg_catalog.pg_class.relname IN ($7)]
[parameters: (0, 'pg_catalog.pg_class', 'r', 'p', 'f', 'pg_catalog', 'tbl')]
(Background on this error at: https://sqlalche.me/e/20/f405)

DuckDB Engine Version

0.13.2

DuckDB Version

1.1.2

SQLAlchemy Version

2.0.36

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Aarya2004
Copy link
Contributor

Aarya2004 commented Oct 25, 2024

Hello! I've taken a look at both this codebase and the sqlalchemy codebase and I think I have an idea for how to fix the problem. My group and I are students at the University of Toronto so we'd love to take a crack at this issue!

@Aarya2004
Copy link
Contributor

Hello @Mause! We were able to make a fix for this but we'd like someone to review the code, is it possible that I could be made the assignee to create the PR?

@NickCrews
Copy link
Contributor

@Aarya2004 you should be able to create the PR without needing to be an assignee. You will need to make a fork of this repo, push your commits to a branch there, and then submit a PR from that branch to here. If you need help with that let me know

@Aarya2004
Copy link
Contributor

Hey @NickCrews, thank you so much! We've created the PR but we'd appreciate a code review if possible. Thanks for the tip!

ccurme added a commit to langchain-ai/langchain that referenced this issue Oct 31, 2024
SQLAlchemy 2.0.36 introduces a regression when creating a table in
DuckDB.

Relevant issues:
- In SQLAlchemy repo (resolution is to update DuckDB):
sqlalchemy/sqlalchemy#12011
- In DuckDB repo (PR is open):
Mause/duckdb_engine#1128

Plan is to track these issues and remove cap when resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants