在Persistent Yesod中做多对多的最佳方法是什么?

时间:2013-04-25 03:14:15

标签: haskell persistent yesod

所以我的/ config / models看起来像这样。

Person
  name Text
Car
  name Text
PersonCar
  personId PersionId eq
  carId CarId eq
  UniquePersonCar personId carId

假设数据库中的输入分别为Person "Batman" Person "Superman" Car "SUV" Car "Ford"

我目前正在执行此操作以将其链接到我的处理程序中。

runDB $ do
  person <- selectFirst [PersonName ==. "Batman"] []
  car    <- selectFirst [Carname ==. "SUV"] []
  let Entity personId _ = case person of
                            Just info -> infor
                            Nothing -> error "no such Person"
  let Entity carId _ = case car of
                            Just info -> infor
                            Nothing -> error "no such Car"
  _ <- insert $ PersonCar personId carId

有更简单的方法吗?是否存在进行此类表达的约定?

2 个答案:

答案 0 :(得分:1)

不,目前还没有这种查询的简写(我至少可以想到)。

答案 1 :(得分:1)

错误调用将暂停您的应用。 logError可能会更好。

这更短:

import Data.Conduit
import qualified Data.Conduit.List as DCL

runDB $ do
    mbPersonId <- runResourceT $ selectKeys [PersonName ==. "Batman"] [] $$ DCL.head
    mbCarId    <- runResourceT $ selectKeys [CarName ==. "SUV"] [] $$ DCL.head

    case (mbPersonId, mbCarId) of
        (Just personId, Just carId) -> do
              _ <- insert $ PersonCar personId carId
              return ()

        _ -> $(logError) "error looking for Batman and SUV"
相关问题