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

Fix bind reflection #1052

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/flask_sqlalchemy/__init__.py
Expand Up @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions tests/test_binds.py
Expand Up @@ -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