3

In my application I need to be able to write a query that takes a list of IDs and returns a list of each of those records.

From what I can tell from the yesod persistent page I could do something like

selectList (UserId ==. 1 ||. UserId ==. 2 ||. UserId ==. 3) []

Which I believe would return a list containing user 1, 2 and 3 but I can't work out how I would write this query when I don't know the list or how long it will be at compile time.

How would I go about selecting a list of records using a list of IDs in Haskell persistent.

Qwertie
  • 5,784
  • 12
  • 45
  • 89

1 Answers1

3

Persistent has several combinators for creating queries the one you are looking for is (<-.) :: PersistField typ => EntityField v typ -> [typ] -> Filter v.

Then your query can be simplified to

selectList [UserId <-. [1..3]] []
epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
  • 1
    I got it working but I had to change it to `selectList [UserId <-. users] []` where users is of the type [UserId]. If you could edit the answer to that I will accept it. – Qwertie Jan 18 '18 at 07:28
  • @qwertie fixed it, sorry for the typo – epsilonhalbe Jan 18 '18 at 07:41
  • 1
    It also can't be a list of ints, has to be a list of UserId or whatever type was created for the model ID. – Qwertie Jan 18 '18 at 10:12