Skip to content

Commit 21f0ff3

Browse files
committed
fix: Compatible with earlier versions, the transaction is auto commit after the role user is created.
1 parent e2fa810 commit 21f0ff3

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

fastapi_user_auth/auth/auth.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,11 @@ def _create_role_user_sync(self, session: Session, role_key: str = "admin") -> U
224224
session.flush()
225225
return user
226226

227-
async def create_role_user(self, role_key: str = "admin") -> User:
228-
return await self.db.async_run_sync(self._create_role_user_sync, role_key)
227+
async def create_role_user(self, role_key: str = "admin", commit: bool = True) -> User:
228+
user = await self.db.async_run_sync(self._create_role_user_sync, role_key)
229+
if commit:
230+
await self.db.async_commit()
231+
return user
229232

230233

231234
class AuthRouter(RouterMixin):

tests/test_auth/test_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
async def test_create_role_user(auth: Auth):
99
user = await auth.create_role_user("admin2")
10+
await auth.db.async_refresh(user)
1011
assert user.username == "admin2"
11-
await auth.db.async_commit()
1212
# test user roles
1313
stmt = select(User).options(selectinload(User.roles)).where(User.username == "admin2")
1414
result = await auth.db.async_scalar(stmt)

0 commit comments

Comments
 (0)