Skip to content

Commit f392a5f

Browse files
Fix incorrect async database documentation (#1531)
- Remove incorrect 'await' statements from result.all() and result.first() calls - These methods are synchronous and return data directly after await session.exec(stmt) - Update alert box to reflect correct synchronous nature of result methods - Preserve correct async patterns for session operations (execute, commit, delete, refresh) Fixes 5 instances of incorrect await usage in async database examples Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: [email protected] <[email protected]>
1 parent 07db64a commit f392a5f

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

docs/database/queries.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class AsyncUserState(rx.State):
211211
async with rx.asession() as asession:
212212
result = await asession.execute(User.select())
213213
async with self:
214-
self.users = (await result.all())
214+
self.users = result.all()
215215
```
216216

217217
### Async Select
@@ -229,7 +229,7 @@ class AsyncQueryUser(rx.State):
229229
stmt = User.select().where(User.username.contains(self.name))
230230
result = await asession.execute(stmt)
231231
async with self:
232-
self.users = (await result.all())
232+
self.users = result.all()
233233
```
234234

235235
### Async Insert
@@ -262,7 +262,7 @@ class AsyncChangeEmail(rx.State):
262262
async with rx.asession() as asession:
263263
stmt = User.select().where(User.username == self.username)
264264
result = await asession.execute(stmt)
265-
user = (await result.first())
265+
user = result.first()
266266
if user:
267267
user.email = self.email
268268
asession.add(user)
@@ -282,7 +282,7 @@ class AsyncRemoveUser(rx.State):
282282
async with rx.asession() as asession:
283283
stmt = User.select().where(User.username == self.username)
284284
result = await asession.execute(stmt)
285-
user = (await result.first())
285+
user = result.first()
286286
if user:
287287
await asession.delete(user)
288288
await asession.commit()
@@ -331,14 +331,14 @@ class AsyncRawSQL(rx.State):
331331
async with rx.asession() as asession:
332332
result = await asession.execute("SELECT * FROM user")
333333
async with self:
334-
self.users = [list(row) for row in (await result.all())]
334+
self.users = [list(row) for row in result.all()]
335335
```
336336

337337
```md alert info
338338
# Important Notes for Async Database Operations
339339
- Always use the `@rx.event(background=True)` decorator for async event handlers
340340
- Most operations against the `asession` must be awaited, including `commit()`, `execute()`, `refresh()`, and `delete()`
341341
- The `add()` method does not need to be awaited
342-
- Result objects from queries have methods like `all()` and `first()` that must be awaited
342+
- Result objects from queries have methods like `all()` and `first()` that are synchronous and return data directly
343343
- Use `async with self:` when updating state variables in background tasks
344344
```

0 commit comments

Comments
 (0)