Skip to content

Commit 1f72d96

Browse files
authored
skip invalid default bind (#1119)
2 parents 44eaa5e + 64bdde6 commit 1f72d96

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Unreleased
55

66
- Export typing information instead of using external typeshed definitions.
77
:issue:`1112`
8+
- If default engine options are set, but ``SQLALCHEMY_DATABASE_URI`` is not set, an
9+
invalid default bind will not be configured. :issue:`1117`
810

911

1012
Version 3.0.0

src/flask_sqlalchemy/extension.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def init_app(self, app: Flask) -> None:
294294
if basic_uri is not None:
295295
basic_engine_options["url"] = basic_uri
296296

297-
if basic_engine_options:
297+
if "url" in basic_engine_options:
298298
engine_options.setdefault(None, {}).update(basic_engine_options)
299299

300300
if not engine_options:

tests/test_engine.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_url_type(app: Flask, value: str | sa.engine.URL) -> None:
6868
assert str(db.engines["a"].url) == "sqlite://"
6969

7070

71-
def test_no_default_url(app: Flask) -> None:
71+
def test_no_binds_error(app: Flask) -> None:
7272
del app.config["SQLALCHEMY_DATABASE_URI"]
7373

7474
with pytest.raises(RuntimeError) as info:
@@ -78,6 +78,15 @@ def test_no_default_url(app: Flask) -> None:
7878
assert str(info.value) == e
7979

8080

81+
@pytest.mark.usefixtures("app_ctx")
82+
def test_no_default_url(app: Flask) -> None:
83+
del app.config["SQLALCHEMY_DATABASE_URI"]
84+
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"}
85+
db = SQLAlchemy(app, engine_options={"echo": True})
86+
assert None not in db.engines
87+
assert "a" in db.engines
88+
89+
8190
@pytest.mark.usefixtures("app_ctx")
8291
def test_sqlite_relative_path(app: Flask) -> None:
8392
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"

0 commit comments

Comments
 (0)