Skip to content

Commit 52e7992

Browse files
authored
Merge pull request #547 from davidism/get_app
pass real app instead of current_app to connector
2 parents fa8f9bf + d18f7f7 commit 52e7992

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Changelog
22
=========
33

4+
45
Version 2.3.0
56
-------------
67

@@ -23,10 +24,15 @@ In development
2324
removed in 3.0. (`#546`_)
2425
- Models have a default ``repr`` that shows the model name and primary key.
2526
(`#530`_)
27+
- Fixed a bug where using ``init_app`` would cause connectors to always use the
28+
``current_app`` rather than the app they were created for. This caused issues
29+
when multiple apps were registered with the extension. (`#547`_)
2630

2731
.. _#530: https://github.com/mitsuhiko/flask-sqlalchemy/pull/530
2832
.. _#541: https://github.com/mitsuhiko/flask-sqlalchemy/pull/541
2933
.. _#546: https://github.com/mitsuhiko/flask-sqlalchemy/pull/546
34+
.. _#547: https://github.com/mitsuhiko/flask-sqlalchemy/pull/547
35+
3036

3137
Version 2.2
3238
-----------

flask_sqlalchemy/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,14 +887,14 @@ def get_app(self, reference_app=None):
887887
return reference_app
888888

889889
if current_app:
890-
return current_app
890+
return current_app._get_current_object()
891891

892892
if self.app is not None:
893893
return self.app
894894

895895
raise RuntimeError(
896-
'application not registered on db instance and no application'
897-
'bound to current context'
896+
'No application found. Either work inside a view function or push'
897+
' an application context.'
898898
)
899899

900900
def get_tables_for_bind(self, bind=None):

tests/test_binds.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import flask_sqlalchemy as fsa
2+
13

24
def test_basic_binds(app, db):
35
app.config['SQLALCHEMY_BINDS'] = {
@@ -78,3 +80,14 @@ class FooBoundModel(AbstractFooBoundModel):
7880
metadata.reflect(bind=db.get_engine(app, 'foo'))
7981
assert len(metadata.tables) == 1
8082
assert 'foo_bound_model' in metadata.tables
83+
84+
85+
def test_connector_cache(app):
86+
db = fsa.SQLAlchemy()
87+
db.init_app(app)
88+
89+
with app.app_context():
90+
db.get_engine()
91+
92+
connector = fsa.get_state(app).connectors[None]
93+
assert connector._app is app

0 commit comments

Comments
 (0)