Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So my /config/models looks like this.

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.

I'm currently doing this to link them up 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 a convention for doing such expression?

like image 699
HHC Avatar asked Apr 25 '13 03:04

HHC


1 Answers

No, there's currently no shorthand for this kind of a query (that I can think of, at least).

like image 66
Michael Snoyman Avatar answered Sep 22 '22 11:09

Michael Snoyman