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

Question: Any way to create engine from existing connection? #1143

Open
1 task done
NickCrews opened this issue Oct 25, 2024 · 2 comments
Open
1 task done

Question: Any way to create engine from existing connection? #1143

NickCrews opened this issue Oct 25, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@NickCrews
Copy link
Contributor

What happened?

See duckdb/duckdb#14549 (comment). Any way to go from duckdb.DuckDBPyConnection to a sqlalchemy.engine? I have to make my own little wrapper class that provides the needed methods for sqlalchemy? Thanks!

DuckDB Engine Version

NA

DuckDB Version

NA

SQLAlchemy Version

No response

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Mause
Copy link
Owner

Mause commented Oct 27, 2024

I can't say I'd really recommend this, but you can do this to work around that error:

from duckdb_engine import ConnectionWrapper

engine = sa.create_engine("duckdb://", creator=lambda: ConnectionWrapper(con))

this class just patches the duckdb connection to be what sqlalchemy expects from postgres/psycopg2

@NickCrews
Copy link
Contributor Author

Thanks!

I can't say I'd really recommend this,

because you'd like to keep ConnectionWrapper as non-public, and you might make breaking changes at any time? That seems like a good idea.

What would you think about if you encapsulated that into a more simple API, and then exposed that publicly:

def create_engine(con: duckdb.DuckDBPyConnection) -> sqlalchemy.engine.Engine:
    return sa.create_engine("duckdb://", creator=lambda: ConnectionWrapper(con))

I'm not that familiar with sqlalchemy's API though, are users then gonna want to pass in various kwargs into creat_engine?

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

No branches or pull requests

2 participants