Skip to content

Commit 83402a0

Browse files
committed
update docs
1 parent ace0638 commit 83402a0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Loading

docs/questions.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,34 @@ title: 常见问题
2121
假设我们直接在数据库中修改了某些数据,但调用接口后发现返回结果未发生变化。返回数据可能来源于 Redis
2222
缓存,而通过数据库直接修改的操作不会触发缓存的自动更新。因此,返回数据看似未受影响。解决方法是手动清理 Redis
2323
中的相关缓存,之后数据将正确反映修改结果
24+
25+
## can't call await_only() here.
26+
27+
```json
28+
{
29+
"code": 500,
30+
"msg": "(sqlalchemy.exc.MissingGreenlet) greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place?\n[SQL: SELECT sys_dict_data.id AS sys_dict_data_id, sys_dict_data.label AS sys_dict_data_label, sys_dict_data.value AS sys_dict_data_value, sys_dict_data.sort AS sys_dict_data_sort, sys_dict_data.status AS sys_dict_data_status, sys_dict_data.remark AS sys_dict_data_remark, sys_dict_data.type_id AS sys_dict_data_type_id, sys_dict_data.created_time AS sys_dict_data_created_time, sys_dict_data.updated_time AS sys_dict_data_updated_time \nFROM sys_dict_data \nWHERE %s = sys_dict_data.type_id]\n[parameters: [{'%(2071788311008 param)s': 1}]]\n(Background on this error at: https://sqlalche.me/e/20/xd2s)",
31+
"data": null,
32+
"trace_id": "89afd9b0f2b8442590661701e2b6b495"
33+
}
34+
```
35+
36+
![await_only](/images/sqlalchemy_await_only.png)
37+
38+
在 SQLAlchemy 2.0 中异步中,关系(relationship)表默认使用
39+
[懒加载](https://docs.sqlalchemy.org/en/20/glossary.html#term-lazy-loading),所以,如果你未在 ORM 语句中添加关联字段的
40+
加载策略,那么关联字段可能被定义为错误(如上图所示),此时如果调用 pydantic / fastapi 序列化,那么将触发字段错误,因为字段本身就是个错误
41+
42+
可用的解决方案有多种,请阅读 SQLA 官方文档,fba 默认使用 `noload()` 对此进行处理,例如:
43+
44+
```python
45+
stmt = ( # [!code word:noload]
46+
select(self.model)
47+
.options(
48+
selectinload(self.model.dept).options(noload(Dept.parent), noload(Dept.children), noload(Dept.users)),
49+
noload(self.model.socials),
50+
selectinload(self.model.roles).options(noload(Role.users), noload(Role.menus), noload(Role.rules)),
51+
)
52+
.order_by(desc(self.model.join_time))
53+
)
54+
```

0 commit comments

Comments
 (0)