简体   繁体   English

Haskell Esqueleto-也许键不绑定到LeftOuterJoin

[英]Haskell Esqueleto - Maybe Keys don't bind on LeftOuterJoin

I am with some trouble while making a join which uses a maybe foreign key . 在使用maybe foreign keymaybe 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: 我尝试了Esqueleto显示的Esqueleto连接,但是出现以下错误:

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.^. , E.just and E.?. E.justE.?. would improve my Esqueleto usage. 会提高我的Esqueleto使用率。

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.?. 必须使类型匹配,因为我们使用的是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 使用已经存在的Maybe值,因此,我们必须将右侧Maybe (Maybe AType) ,否则将是错误的,并且我只使用E.just . E.just E.just . E.just which will wrap two times. E.just . E.just将包装两次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM