diff --git a/CHANGES.rst b/CHANGES.rst index a6f2ef28..f1f04efa 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -23,6 +23,7 @@ Unreleased design issues that are difficult to debug. Call ``db.session.commit()`` directly instead. :issue:`216` - Change the default MySQL character set to "utf8mb4". :issue:`875` +- Add `bind_key` to reflected tables. :issue:`660` Version 2.5.1 diff --git a/src/flask_sqlalchemy/__init__.py b/src/flask_sqlalchemy/__init__.py index 192788af..38e2f5cd 100644 --- a/src/flask_sqlalchemy/__init__.py +++ b/src/flask_sqlalchemy/__init__.py @@ -1041,6 +1041,8 @@ def _execute_for_all_tables(self, app, bind, operation, skip_tables=False): if not skip_tables: tables = self.get_tables_for_bind(bind) extra["tables"] = tables + if operation == "reflect": + extra["info"] = {"bind_key": bind} op = getattr(self.Model.metadata, operation) op(bind=self.get_engine(app, bind), **extra) diff --git a/tests/test_binds.py b/tests/test_binds.py index 4a063b5b..29a04cbd 100644 --- a/tests/test_binds.py +++ b/tests/test_binds.py @@ -124,3 +124,27 @@ def test_execute_with_binds_arguments(app, db): db.session.execute( "SELECT true", bind_arguments={"bind": db.get_engine(app, "foo")} ) + + +def test_reflect(app, db): + app.config["SQLALCHEMY_BINDS"] = {"foo": "sqlite://"} + db.get_engine(app, "foo").execute("CREATE TABLE foo_table (id INTEGER PRIMARY KEY)") + db.get_engine(app).execute( + "CREATE TABLE default_bind_table (id INTEGER PRIMARY KEY)" + ) + + db.reflect("foo", app=app) + + assert db.metadata.tables.get("default_bind_table") is None + + foo_table = db.metadata.tables.get("foo_table") + assert foo_table is not None + assert foo_table in db.get_tables_for_bind("foo") + assert foo_table.info.get("bind_key") == "foo" + + db.reflect(app=app) + + default_bind_table = db.metadata.tables.get("default_bind_table") + assert default_bind_table is not None + assert default_bind_table in db.get_tables_for_bind(None) + assert default_bind_table.info.get("bind_key") is None