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

Incompatibility of create_database() from sqlalchemy-utils 0.39.0 with SQLAlchemy 2.0.0 #676

Closed
introvenk opened this issue Jan 29, 2023 · 1 comment

Comments

@introvenk
Copy link

introvenk commented Jan 29, 2023

Package / Application versions:

Python version: 3.9.0
sqlalchemy: 2.0.0
sqlalchemy-utils: 0.39.0
postgres 14

Description

In SQLAlchemy 2.0, Direct engine.execute() calls are no longer supported. The "connectionless" execution pattern, where engine.execute() implicitly procures a Connection object and runs the execute method, has several issues and was not intended for frequent use.

Due to this change that require the use of Connection objects instead of Engine objects, the create_database() function from the sqlalchemy-utils library has become incompatible with SQLAlchemy 2.0

The failure is caused by the get_bind() function not returning a bind for the Engine object and the subsequent failure of the quote() function to determine the dialect and ultimately leading to the failure of the create_database() function. A check in the quote() function to see if the input is an Engine object and using the dialect attribute directly will resolve this issue.

Steps to Reproduce

  1. Install the latest version of SQLAlchemy and SQLAlchemy-utils
  2. Try to use the create_database() function from sqlalchemy-utils on an SQLAlchemy 2.0 Engine object.
  3. Observe the failure of the quote() function due to the failure of get_bind() to return a bind for the Engine object.

Expected Result

The create_database() function should successfully execute the create database statement.

Actual Result

When using the create_database() function from the sqlalchemy-utils library, the underlying quote() function that create_database() depends upon fails and raises an error because it cannot determine the dialect from the Engine object provided to it, this is because the get_bind() function expects the Engine object to have the execute attribute but in SQLAlchemy 2.0 it has been changed. The error message reads as follows:

TypeError: This method accepts only Session, Engine, Connection and declarative model objects.

Solution

To resolve this issue, a check can be added in the quote() function to see if the input is an Engine object and using the dialect attribute on it directly. I am raising a pull request with these changes and would welcome any guidance or suggestions to address the underlying issues with the functions.

introvenk added a commit to introvenk/sqlalchemy-utils that referenced this issue Jan 29, 2023
With SQLAlchemy 2.0 and postgres, 'create_database()'' function from
sqlalchemy-utils was failing and the issue was caused by the failure of
the quote() function to determine the dialect of the database driver.
This was due to the removal of the direct engine.execute() calls in
SQLAlchemy 2.0 which caused the get_bind() function to fail in case
of Engine object.

This change adds a check in the quote() function to see if the input
is an engine object. If it is, the dialect attribute is used directly
instead of trying to use the get_bind() function.
This resolves the issue with create_database()
where the get_bind() function was not able to return a bind for
the engine object and the subsequent failure of the quote() function.

This change allows the 'create_database()'' function from
the sqlalchemy-utils library to be compatible with SQLAlchemy 2.0.0

Fixes kvesteri#676
shuva10v added a commit to re-doubt/ton-indexer that referenced this issue Jan 29, 2023
@kvesteri
Copy link
Owner

Fixed in #690

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants