flask-sqlalchemy
flask-sqlalchemy copied to clipboard
Possible issue on session get_bind
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
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.
Sure @davidism I'll dig into it. At first it seemed like something was obviously wrong with what I was doing
I looked at the code briefly and didn't see anything. Check the sqlalchemy version, might be a compatibility issue?
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
hey this is probably buggy on my end FYI, not sure how to fix yet
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 ?
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
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?
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.
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()
fixed in #1087