What is the best way to do many-to-many in Persistent Yesod?

So my / config / models look like this.

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

Suppose the entries in the database are Person "Batman" Person "Superman" Car "SUV" Car "Ford" respectively.

I am currently doing this to link them in my 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 an agreement for such an expression?

+8
haskell yesod persistent
source share
2 answers

No, there are currently no abbreviations for this type of request (which I can think of, at least).

+1
source share

error calls will stop your application. logError could be better.

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" 
+1
source share

All Articles