flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Possible issue on session get_bind

Open dpgaspar opened this issue 3 years ago • 10 comments

This may be a problem on SQLAlchemy itself, simple example:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"
db = SQLAlchemy(app)

db.session.get_bind()

The output from the execution above:

Traceback (most recent call last):
  File "exp.py", line 8, in <module>
    db.session.get_bind()
  File "<string>", line 2, in get_bind
TypeError: get_bind() takes from 1 to 3 positional arguments but 6 were given

Should return a SQLAlchemy engine

Environment:

  • Python version: 3.7
  • Flask-SQLAlchemy version: 2.5.1
  • SQLAlchemy version: 1.4.7

dpgaspar avatar Apr 12 '21 15:04 dpgaspar

I have absolutely no idea what's going on there, it seems that the frame isn't even available to debug. If you have time, I'd appreciate if you could dig into it, because I won't have time soon.

davidism avatar Apr 12 '21 16:04 davidism

Sure @davidism I'll dig into it. At first it seemed like something was obviously wrong with what I was doing

dpgaspar avatar Apr 12 '21 17:04 dpgaspar

I looked at the code briefly and didn't see anything. Check the sqlalchemy version, might be a compatibility issue?

davidism avatar Apr 12 '21 17:04 davidism

Just a couple of notes:

Using SQLAlchemy directly we don't have any issue:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine("sqlite:///example.sqlite")
session = sessionmaker(bind=engine)()
session.get_bind() 

Using flask-sqlalchemy with SQLAlchemy < 1.4.0:

db.session.get_bind debug:

return getattr(self.registry(), name)(*args, **kwargs)

using SQLAlchemy > 1.4.0

    @property
    def _proxied(self):
        return self.registry()

https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/orm/session.py#L4026

dpgaspar avatar Apr 13 '21 10:04 dpgaspar

hey this is probably buggy on my end FYI, not sure how to fix yet

zzzeek avatar Apr 15 '21 15:04 zzzeek

Leaving an update here: re-checked this issue with latest SQLAlchemy=1.4.18 and Flask-SQLAlchemy 2.5.1, are still an issue but with a different error:

    db.session.get_bind()
  File "<string>", line 2, in get_bind
TypeError: get_bind() got an unexpected keyword argument 'bind'

No problem on Flask-SQLAlchemy current master:

branch 2.x

return SessionBase.get_bind(self, mapper, clause)

current master (bind on kwargs):

return super().get_bind(mapper, **kwargs)

Any ETA for 3.0.0 ?

dpgaspar avatar Jun 15 '21 14:06 dpgaspar

hi, was just wondering what the next steps are for this? do we need something like 2.5.2 with #943 included in the release?

edit: originally linked to wrong PR

kanetkarster avatar Aug 28 '21 22:08 kanetkarster

Hello, I understand that this is fixed in master in #943; any update on getting a formal release (whether 2.5.2 or 3.0.0) containing the fix?

jnahmias avatar Dec 06 '21 17:12 jnahmias

Hello, I understand that this is fixed in master in #943; any update on getting a formal release (whether 2.5.2 or 3.0.0) containing the fix?

Please see this comment.

tachyondecay avatar Dec 06 '21 23:12 tachyondecay

I've been getting a related error with code such as

entries = db.session.execute(s,  bind=db.get_engine(bind="name")).all()

which works with sqlalchemy 1.3.24 but fails with sqlalchemy 1.4.31 with

TypeError: get_bind() got an unexpected keyword argument 'bind'

Unfortunately we rely heavily on pandas and pandas 1.4 raises an error unless sqlalchemy is >=1.4, so simply holding sqlalchemy back is more broadly problematic. However, while waiting for the update to flask-sqlalchemy I was able to work around it by substituting the code above with

engine = db.get_engine(bind="name")
session = db.create_scoped_session(options={'bind': engine})
entries = session.execute(s).all()

RomeshA avatar Feb 01 '22 04:02 RomeshA

fixed in #1087

davidism avatar Sep 18 '22 17:09 davidism