Skip to content

Commit 9184b36

Browse files
committed
fix: Failed to destroy a context variable in a fastapi dependency.
1 parent af58a10 commit 9184b36

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

sqlalchemy_database/database.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ async def session_generator(self) -> AsyncGenerator[AsyncSession, Any]:
145145
yield self.session
146146
else:
147147
"""If the current context has no session, create a new session."""
148-
async with self() as session:
148+
async with self.session_maker() as session:
149149
yield session
150+
if self.commit_on_exit:
151+
await session.commit()
150152

151153
async def run_sync(
152154
self,
@@ -226,8 +228,10 @@ def session_generator(self) -> Generator[Session, Any, None]:
226228
yield self.session
227229
else:
228230
"""If the current context has no session, create a new session."""
229-
with self() as session:
231+
with self.session_maker() as session:
230232
yield session
233+
if self.commit_on_exit:
234+
session.commit()
231235

232236
def run_sync(
233237
self,

0 commit comments

Comments
 (0)