如何在 PonyORM 的 "ON" cause of "LEFT JOIN" 中添加条件

How to add a condition in "ON" cause of "LEFT JOIN" in PonyORM

我想运行在PonyORM中进行以下查询。

SELECT af.AppFormID, af.AppFormTitle, ra.CreateGrant, ra.ReadGrant, ra.UpdateGrant, ra.DeleteGrant, ra.PrintGrant
FROM public.appforms as af left join public.roleaccesses as ra 
on af.appformid = ra.appformid and ra.roleid = 2

如果你看到代码的最后一部分,我在 "ON" 原因中添加了一个条件。

我试着在python中写了下面的代码。

query= orm.left_join((af.AppFormID, af.AppFormTitle, ra.CreateGrant, ra.ReadGrant, ra.UpdateGrant, ra.DeleteGrant, ra.PrintGrant) for af in AppForms for ra in af.RoleAccess if ra.RoleID.RoleID == id)

但是,"if"被称为"WHERE"原因。我该如何解决这个问题?

感谢您的帮助。

当前版本的 PonyORM (0.7.6) 不支持在 LEFT JOIN 子句中附加条件。但我同意这是一个有用的功能。

请在此处添加新问题:https://github.com/ponyorm/pony/issues

我不保证会很快实现,但最终我们会添加它。

在此之前,您需要编写原始 SQL 查询:

role_id = 2

rows = db.select("""
    SELECT af.AppFormID, af.AppFormTitle,
           ra.CreateGrant, ra.ReadGrant,
           ra.UpdateGrant, ra.DeleteGrant, ra.PrintGrant
    FROM public.appforms as af
      LEFT JOIN public.roleaccesses as ra
        ON af.appformid = ra.appformid and ra.roleid = $role_id
""")