在多个关系层面的情况下,在sqlalchemy(async)中查询数据的正确方法是什么?
我正在尝试从相关表中读取数据,这些表的深度为 x 级别并且具有指定的关系,即:
table A
table B
table C
table ABC
表 ABC 具有关系 ABC.a = A、ABC.b = B 和 ABC.c = C, 即外键 ABC.aid = A.id、ABC.bid = B.id 和 ABC.cid = C.id。
- ABC 中的援助、出价和 cid 使用 UniqueContstraint 关系设置为唯一,
- 关系是使用lazy="joined"
当我执行 select(ABC) 时,我能够从 ABC 以及相关表中获取所有值,即:
{ABC.a: {A}, ABC.b: {B}, ABC.c: {C}}
我还有表 D它与 ABC (D.abcid = ABC.id) 有关系,我努力构建一个正确的选择语句,该语句将为我提供来自 A、B 和 C 的所有数据。实际上,我不确定这是否可行,或者我错过/没有了解文档中的某些内容,因为我尝试了各种加载策略,为 D 和 ABC 表指定了 join_depth 等。无论我得到什么:
sqlalchemy.exc.InvalidRequestError:必须对此调用 unique() 方法结果,因为它包含包含针对集合的加入的急切加载的结果
我想以与第一级关系相同的方式获取数据,即:
{D.abc : {ABC.a: {A}, ABC.b: {B}, ABC.c: {C}}}
是否可能或者我必须完全更改选择查询创建多个加入并手动选择我需要的所有值? 当我只需获取生成的 select 语句并直接在 DB shell (MariaDB) 中使用它时,我就能够从数据库中获取正确的记录,因此我认为唯一的问题是我对 SQL 如何处理/呈现这些记录缺乏了解内部。
I'm trying to read data from related tables which are x levels deep and which have relationships specified, i.e.:
table A
table B
table C
table ABC
Table ABC has relationships ABC.a = A, ABC.b = B and ABC.c = C,
i.e. foreign keys ABC.aid = A.id, ABC.bid = B.id and ABC.cid = C.id.
- aid, bid and cid in ABC are set unique using UniqueContstraint
- relationship is using lazy="joined"
When I do select(ABC) I'm able to get all values from ABC and also from related tables, i.e.:
{ABC.a: {A}, ABC.b: {B}, ABC.c: {C}}
I have also table D which has a relationship to ABC (D.abcid = ABC.id) and I struggle to construct a correct select statement which would give me all data also from A, B and C. Actually I'm not sure if this should work or I missed / do not understand something in the documentation as I have tried various loading strategies, specified join_depth for D and ABC tables, etc. No matter what I'm getting:
sqlalchemy.exc.InvalidRequestError: The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collections
I would like to get the data the same way as for 1st level relationship, i.e.:
{D.abc : {ABC.a: {A}, ABC.b: {B}, ABC.c: {C}}}
Is it possible or do I have to change the select query completely and just create multiple joins and manually pick all the values I need?
I'm able to get correct records from the database when I just take the generated select statement and use it directly in a DB shell (MariaDB) so I assume that the only issue is my lack of understanding of how SQL handles/presents these records internally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是在其中一个模型中使用
useList = true
,所有关系都可以完美地工作到现在的最低水平。The issue was using
uselist=True
in one of the models, all relationships are working perfectly down to the lowest level now.