-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The PR enables Flask support by changing how we validate the session object. In normal Sessions, the `bind` attribute is never empty. However, when using scoped sessions, this is intended to be empty. This change will call the `get_bind()` function when the bind is not set.
- Loading branch information
Showing
5 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from sqlalchemy.orm import Mapped, sessionmaker | ||
|
||
from advanced_alchemy import SQLAlchemySyncRepository | ||
from advanced_alchemy.base import UUIDBase | ||
|
||
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:" | ||
|
||
app = Flask(__name__) | ||
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI | ||
db = SQLAlchemy(app) | ||
|
||
|
||
class Message(UUIDBase): | ||
text: Mapped[str] | ||
|
||
|
||
class MessageRepository(SQLAlchemySyncRepository[Message]): | ||
model_type = Message | ||
|
||
|
||
# Working | ||
with app.app_context(): | ||
with db.engine.begin() as conn: | ||
Message.metadata.create_all(conn) | ||
|
||
session = sessionmaker(db.engine)() | ||
|
||
repo = MessageRepository(session=session) | ||
repo.add(Message(text="Hello, world!")) | ||
|
||
message = repo.list()[0] | ||
assert message.text == "Hello, world!" # noqa: S101 | ||
|
||
|
||
# Not working | ||
with app.app_context(): | ||
with db.engine.begin() as conn: | ||
Message.metadata.create_all(conn) | ||
|
||
session = db.session | ||
repo = MessageRepository(session=session) | ||
repo.add(Message(text="Hello, world!")) | ||
|
||
message = repo.list()[0] | ||
assert message.text == "Hello, world!" # noqa: S101 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters