I am with some trouble while making a join which uses a maybe foreign key
.
My Models:
OrderItem
productSnapshot ProductSnapshotId
ProductFlow
productInstance ProductInstanceId
orderItem OrderItemId Maybe
ProductSnapshot
productInstance ProductInstanceId
ProductInstance
code Text
And my joins:
type Entities = [(Entity OrderItem, Entity ProductSnapshot, Entity ProductInstance, Maybe (Entity ProductFlow))]
loadEntities :: OrderItemId -> Handler Entities
loadEntities oiId = runDB
$ E.select
$ E.from $ \(oi `E.InnerJoin` ps `E.InnerJoin` pi `E.LeftOuterJoin` pf) -> do
E.on $ pf E.?. ProductFlowOrderItem E.==. E.just (oi ^. OrderItemId)
E.on $ pi ^. ProductInstanceId E.==. ps ^. ProductSnapshotProductInstance
E.on $ ps ^. ProductSnapshotId E.==. oi ^. OrderItemProductSnapshot
E.where_ (oi ^. OrderItemId E.==. E.val oiId)
return (oi, ps, pi, pf)
I tried the Esqueleto
join showed above, but I got the following error:
Couldn't match type ‘Key OrderItem’ with ‘Maybe (Key OrderItem)’
Expected type: EntityField OrderItem (Maybe (Key OrderItem))
Actual type: EntityField OrderItem (Key OrderItem)
In the second argument of ‘(^.)’, namely ‘OrderItemId’
In the first argument of ‘E.just’, namely ‘(oi ^. OrderItemId)’
I think it is caused because I'm using a maybe foreign key. I tried other ways, and got strange type errors. I think I don't understand completely well how these joins work, so a brief explanation about E.^.
, E.just
and E.?.
would improve my Esqueleto usage.
I managed to understand the problem and solve it myself, instead of
E.on $ pf E.?. ProductFlowOrderItem E.==. E.just (oi ^. OrderItemId)
use this
E.on $ pf E.?. ProductFlowOrderItem E.==. (E.just . E.just) (oi ^. OrderItemId)
It is necessary to make the types match, because we are using E.?.
with an already Maybe
value, so, we have to make the right side a Maybe (Maybe AType)
, otherwise it would be wrong, and to make it I just used E.just . E.just
E.just . E.just
which will wrap two times.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.