【发布时间】:2015-01-24 01:10:12
【问题描述】:
我有一个包含教师、学校和学区的数据库架构。 TEACHERS 表有一个可以为空的SCHOOL_ID 列(老师可能属于也可能不属于学校),SCHOOLS 表有一个可以为空的DISTRICT_ID 列(学校可能属于也可能不属于学区)。
使用 Esqueleto,我想要一个教师列表,每个教师都有一个学校(如果他们属于一个学校)和一个学区(如果他们属于一个属于某个学区的学校)。花了一点时间才弄清楚老师的正确表达方式->学校左连接,但我最终还是正确的:
select $
from $ \(teacher `LeftOuterJoin` school) -> do
on (teacher ^. TeacherSchoolId ==. school ?. SchoolId)
return (teacher, school)
我尝试使用类似的表达式在 DISTRICTS 添加另一个左连接:
select $
from $ \(teacher `LeftOuterJoin` school `LeftOuterJoin` district) -> do
on (school ^. SchoolDistrictId ==. district ?. DistrictId)
on (teacher ^. TeacherSchoolId ==. school ?. SchoolId)
return (teacher, school, district)
但我得到一个错误:
Couldn't match type ‘Entity School’ with ‘Maybe (Entity School)’
Expected type: SqlExpr (Maybe (Entity School))
Actual type: SqlExpr (Entity School)
In the first argument of ‘(?.)’, namely ‘school’
In the second argument of ‘(==.)’, namely ‘school ?. SchoolId’
这个双重连接可以用 Esqueleto 来表达吗?如果有,怎么做?
【问题讨论】:
-
我不知道这个库,它看起来很复杂,但看起来你需要提升一些
Maybe的东西才能与 SQL 的东西同步。或者反过来。我不知道。