Skip to content

Commit

Permalink
fix: get_bind signature
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed Apr 15, 2021
1 parent e6d7642 commit 070bf10
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ Unreleased
``db.session.commit()`` directly instead. :issue:`216`


Version 2.5.2
-------------

Unreleased

- Fix session ``get_bind`` signature for SQLAlchemy 1.4. :issue:`953`


Version 2.5.1
-------------

Expand Down
17 changes: 15 additions & 2 deletions src/flask_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,14 @@ def __init__(self, db, autocommit=False, autoflush=True, **options):
**options,
)

def get_bind(self, mapper=None, **kwargs):
def get_bind(
self,
mapper=None,
clause=None,
bind=None,
_sa_skip_events=None,
_sa_skip_for_implicit_returning=False,
):
"""Return the engine or connection for a given model or
table, using the ``__bind_key__`` if it is set.
"""
Expand All @@ -191,7 +198,13 @@ def get_bind(self, mapper=None, **kwargs):
state = get_state(self.app)
return state.db.get_engine(self.app, bind=bind_key)

return super().get_bind(mapper, **kwargs)
return super().get_bind(
mapper=mapper,
clause=clause,
bind=bind,
_sa_skip_events=_sa_skip_events,
_sa_skip_for_implicit_returning=_sa_skip_for_implicit_returning,
)


class _SessionSignalEvents:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_sessions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random

import sqlalchemy as sa
from sqlalchemy.engine import Engine
from sqlalchemy.orm import sessionmaker

from flask_sqlalchemy import SQLAlchemy
Expand Down Expand Up @@ -59,3 +60,8 @@ class QazWsx(db.Model):

def test_listen_to_session_event(db):
sa.event.listen(db.session, "after_commit", lambda session: None)


def test_session_get_bind(app, db):
with app.test_request_context():
assert isinstance(db.session.get_bind(), Engine)

0 comments on commit 070bf10

Please sign in to comment.