SQLAlchemy 2 and Dialect’s has_schema()

505 views
Skip to first unread message

jens.t...@gmail.com

unread,
Jul 31, 2022, 10:15:49 PM7/31/22
to sqlalchemy
Hello,

I’m curious to get recommendations about the following use-case (based on this SO Answer):

>>> import sqlalchemy
>>> configuration = {'sqlalchemy.url': 'postgresql+psycopg2://postgres:postgres@localhost/test_db'}
>>> engine = sqlalchemy.engine_from_config(configuration, future=True)
>>> engine.dialect.has_schema(engine, schema)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/base.py", line 3360, in has_schema
    cursor = connection.execute(
  File "/.../lib/python3.10/site-packages/sqlalchemy/future/engine.py", line 320, in _not_implemented
    raise NotImplementedError(
NotImplementedError: This method is not implemented for SQLAlchemy 2.0.


This works if I turn off the future flag:

>>> engine = sqlalchemy.engine_from_config(configuration)
>>> engine.dialect.has_schema(engine, schema)
False


Is there a fix coming? Is there an alternative suggestion? Interestingly, I wasn’t able to find has_schema() in SQLA v1.4 (although v0.7 says it was added) but only for SQLA v2.

For now I can use an “old” engine to check for schema, and the switch to a new one, but what’s the current recommendation?

Much thanks!
Jens

Mike Bayer

unread,
Aug 1, 2022, 11:33:33 AM8/1/22
to noreply-spamdigest via sqlalchemy


On Sun, Jul 31, 2022, at 10:15 PM, jens.t...@gmail.com wrote:
Hello,

I’m curious to get recommendations about the following use-case (based on this SO Answer):

>>> import sqlalchemy
>>> configuration = {'sqlalchemy.url': 'postgresql+psycopg2://postgres:postgres@localhost/test_db'}
>>> engine = sqlalchemy.engine_from_config(configuration, future=True)
>>> engine.dialect.has_schema(engine, schema)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/.../lib/python3.10/site-packages/sqlalchemy/dialects/postgresql/base.py", line 3360, in has_schema
    cursor = connection.execute(
  File "/.../lib/python3.10/site-packages/sqlalchemy/future/engine.py", line 320, in _not_implemented
    raise NotImplementedError(
NotImplementedError: This method is not implemented for SQLAlchemy 2.0.

This works if I turn off the future flag:

>>> engine = sqlalchemy.engine_from_config(configuration)
>>> engine.dialect.has_schema(engine, schema)
False

Is there a fix coming? Is there an alternative suggestion? Interestingly, I wasn’t able to find has_schema() in SQLA v1.4 (although v0.7 says it was added) but only for SQLA v2.

it's because that is not user-facing API, that's for dialect authors and 1.4 does not have official public API for this function.



For now I can use an “old” engine to check for schema, and the switch to a new one, but what’s the current recommendation?

Much thanks!
Jens


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages