[英]What's the best way to do many-to-many in Persistent Yesod?
So my /config/models looks like this. 所以我的/ config / models看起来像这样。
Person
name Text
Car
name Text
PersonCar
personId PersionId eq
carId CarId eq
UniquePersonCar personId carId
Assume the inputs in the database are Person "Batman"
Person "Superman"
Car "SUV"
Car "Ford"
respectively. 假设数据库中的输入分别是Person "Batman"
Person "Superman"
Car "SUV"
Car "Ford"
。
I'm currently doing this to link them up in my Handler. 我目前正在这样做,以将它们链接到我的Handler中。
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
Is there an easier way to do this? 有没有更简单的方法可以做到这一点? Is there a convention for doing such expression? 有进行这种表达的约定吗?
不,目前没有这种查询的捷径(至少我能想到)。
calls to error will halt your app. 错误调用将暂停您的应用。 logError may be better. logError可能更好。
This is shorter: 这更短:
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.