diff --git a/CHANGES.rst b/CHANGES.rst index d349f2de..4e521393 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,6 +5,8 @@ Unreleased - Export typing information instead of using external typeshed definitions. :issue:`1112` +- If default engine options are set, but ``SQLALCHEMY_DATABASE_URI`` is not set, an + invalid default bind will not be configured. :issue:`1117` Version 3.0.0 diff --git a/src/flask_sqlalchemy/extension.py b/src/flask_sqlalchemy/extension.py index 3fc2689e..95df831e 100644 --- a/src/flask_sqlalchemy/extension.py +++ b/src/flask_sqlalchemy/extension.py @@ -294,7 +294,7 @@ def init_app(self, app: Flask) -> None: if basic_uri is not None: basic_engine_options["url"] = basic_uri - if basic_engine_options: + if "url" in basic_engine_options: engine_options.setdefault(None, {}).update(basic_engine_options) if not engine_options: diff --git a/tests/test_engine.py b/tests/test_engine.py index a4d37729..40a3b4e4 100644 --- a/tests/test_engine.py +++ b/tests/test_engine.py @@ -68,7 +68,7 @@ def test_url_type(app: Flask, value: str | sa.engine.URL) -> None: assert str(db.engines["a"].url) == "sqlite://" -def test_no_default_url(app: Flask) -> None: +def test_no_binds_error(app: Flask) -> None: del app.config["SQLALCHEMY_DATABASE_URI"] with pytest.raises(RuntimeError) as info: @@ -78,6 +78,15 @@ def test_no_default_url(app: Flask) -> None: assert str(info.value) == e +@pytest.mark.usefixtures("app_ctx") +def test_no_default_url(app: Flask) -> None: + del app.config["SQLALCHEMY_DATABASE_URI"] + app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"} + db = SQLAlchemy(app, engine_options={"echo": True}) + assert None not in db.engines + assert "a" in db.engines + + @pytest.mark.usefixtures("app_ctx") def test_sqlite_relative_path(app: Flask) -> None: app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db"