I'm trying to do a leftJoin
using the following query:
select $ do
(event :& _ :& tags) <- from
$ table @EventRow `leftJoin` table @EventTagRow
`on` ( \(event :& links) ->
event ^. EventRowId ==. links ?. EventTagRowEventId
)
`leftJoin` table @TagRow
`on` ( \(_ :& link :& tag) ->
tag ?. TagRowId ==. link ?. EventTagRowTagId
)
where_ (event ^. EventRowTitle ==. val "one")
pure tags
The problem I have is that the line event ^. EventRowId ==. links ?. EventTagRowEventId
complains that EventRowId
gives an ID type, but ==.
wants Maybe <id type>
. This makes sense, when I look at the definitions of ?.
and ==.
they are:
(?.) :: (PersistEntity val, PersistField typ) => SqlExpr (Maybe (Entity val)) -> EntityField val typ -> SqlExpr (Value (Maybe typ))
(==.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
Which would imply that the LHS and RHS both have to be maybe. Yet the docs for Database.Esqueleto.Experimental
give the following example (see "Example 2: Select with join" in the section "A New Syntax"):
select $ do
(people :& blogPosts) <-
from $ table @Person
`leftJoin` table @BlogPost
`on` (\(people :& blogPosts) ->
people ^. PersonId ==. blogPosts ?. BlogPostAuthorId)
where_ (people ^. PersonAge >. val 18)
pure (people, blogPosts)
Which implies that ==.
can take a SqlExpr (Value typ) -> SqlExpr (Maybe (Value typ))
. Is the documentation wrong or am I doing something wrong?