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

Possible issue on session get_bind #953

Closed
dpgaspar opened this issue Apr 12, 2021 · 11 comments
Closed

Possible issue on session get_bind #953

dpgaspar opened this issue Apr 12, 2021 · 11 comments
Milestone

Comments

@dpgaspar
Copy link

dpgaspar commented Apr 12, 2021

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
@davidism
Copy link
Member

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.

@dpgaspar
Copy link
Author

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

@davidism
Copy link
Member

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

@dpgaspar
Copy link
Author

dpgaspar commented Apr 13, 2021

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

@zzzeek
Copy link

zzzeek commented Apr 15, 2021

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

sqlalchemy-bot pushed a commit to sqlalchemy/sqlalchemy that referenced this issue Jun 3, 2021
Adjusted the means by which classes such as :class:`_orm.scoped_session`
and :class:`_asyncio.AsyncSession` are generated from the base
:class:`_orm.Session` class, such that custom :class:`_orm.Session`
subclasses such as that used by Flask-SQLAlchemy don't need to implement
positional arguments when they call into the superclass method, and can
continue using the same argument styles as in previous releases.

Fixes: #6285
References: pallets-eco/flask-sqlalchemy#953
Change-Id: I8612ab33743625e70eb158efceb0636d783c92a5
@dpgaspar
Copy link
Author

dpgaspar commented Jun 15, 2021

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 ?

@kanetkarster
Copy link

kanetkarster commented Aug 28, 2021

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

@jnahmias
Copy link

jnahmias commented Dec 6, 2021

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?

@tachyondecay
Copy link

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.

@RomeshA
Copy link

RomeshA commented Feb 1, 2022

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()

@davidism
Copy link
Member

fixed in #1087

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

7 participants