【发布时间】:2018-01-16 13:49:15
【问题描述】:
这是我的模型的一个子集:
ServerWebsite
desc Text
url Text
text Text
username Text
password Password
serverDatabaseId ServerDatabaseId Maybe
groupName Text Maybe
serverId ServerId
deriving Show Typeable
ServerDatabase
desc Text
name Text
text Text
username Text
password Password
groupName Text Maybe
serverId ServerId
deriving Show Typeable
我无法构建此查询:
filterServerWebsites :: SqlExpr (Value Text) -> SqlPersistM [Entity ServerWebsite]
filterServerWebsites query = select $ from $ \(w `LeftOuterJoin` db) -> do
on (w ^. ServerWebsiteServerDatabaseId ==. db ?. ServerDatabaseId)
where_ ((w ^. ServerWebsiteDesc `like` query)
||. (w ^. ServerWebsiteUrl `like` query)
||. (w ^. ServerWebsiteText `like` query)
||. (db ?. ServerDatabaseDesc `like` just query))
return w
我不明白构建错误:
• Couldn't match expected type ‘SqlQuery a1’
with actual type ‘(a0 -> b0) -> a0 -> a0 -> c0’
• Probable cause: ‘on’ is applied to too few arguments
In a stmt of a 'do' block:
on (w ^. ServerWebsiteServerDatabaseId ==. db ?. ServerDatabaseId)
和:
• Couldn't match expected type ‘b0 -> b0 -> c0’
with actual type ‘SqlExpr (Value Bool)’
• Possible cause: ‘(==.)’ is applied to too many arguments
In the first argument of ‘on’, namely
‘(w ^. ServerWebsiteServerDatabaseId ==. db ?. ServerDatabaseId)’
我不明白我哪里出错了?
编辑 我是从这段代码开始的,它确实有效:
filterServerWebsites :: SqlExpr (Value Text) -> SqlPersistM [Entity ServerWebsite]
filterServerWebsites query = select $ from $ \w -> do
where_ ((w ^. ServerWebsiteDesc `like` query)
||. (w ^. ServerWebsiteUrl `like` query)
||. (w ^. ServerWebsiteText `like` query))
return w
【问题讨论】:
-
ServerDatabaseId真的是ServerDatabaseServerId吗? -
不,
Server表不相关。这里我们只有ServerWebsite和ServerDatabase。尝试查询ServerWebsite并通过serverDatabaseId ServerDatabaseId Maybe获得ServerDatabase的外部连接。所以ServerDatabaseId是ServerDatabase的PK。 -
嗯,好的。我不是 esqueleto 专家,但我认为
db ?. ServerDatabaseId会访问ServerDatabase中不存在的字段id。 (不过,我可能对此非常错误。) -
据我了解
db是Maybe实体,因为它是外连接,因此正确的表可能没有记录。这就是我认为我们需要?.而不是^.的原因。 -
我不关心
?.,而是关心ServerDatabaseId这个名字,因为上面的ServerDatabase没有id字段,只有一个serverId字段。
标签: haskell yesod persistent esqueleto