title: Persistent - how to filter on a field of a record column
body: <p>I'm in the middle of trying to build my first "real" Haskell app, an API using Servant, where I'm using Persistent for the database backend. But I've run into a problem when trying to use Persistent to make a certain type of database query.</p>
  
  <p>My real problem is of course a fair bit more involved, but the essence of the problem I have run up against can be explained like this. I have a record type such as:</p>
  
  <pre><code>data Foo = Foo { a :: Int, b :: Int }
  
  derivePersistField "Foo"
  </code></pre>
  
  <p>which I am including in an Entity like this:</p>
  
  <pre><code>share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
      Thing
          foo Foo
  |]
  </code></pre>
  
  <p>And I need to be able to query for items in my database for which their Foo value has its <code>a</code> field greater than some <code>aMin</code> that is provided. (The intention is that it will actually be provided by the API request in a query string.)</p>
  
  <p>For ordinary queries, say for an <code>Int</code> field <code>Bar</code>, I could simply do <code>selectList [ThingBar &gt;=. aMin] []</code>, but I'm drawing a blank as to what to put in the filter list in order to extract the field from the record and do a comparison with it. Even though this feels like the sort of thing that Haskell should be able to do rather easily. It feels like there should be a <code>Functor</code> involved here that I can just <code>fmap</code> the <code>a</code> accessor over, but the relevant type, as far as I can tell from the documentation and the <a href="https://www.yesodweb.com/book/persistent" rel="nofollow noreferrer">tutorial</a>, is <code>EntityField Thing</code> defined by a GADT (actually generated by Template Haskell from the <code>share</code> call above), which in this case would have just one constructor yielding an <code>EntityField Thing Foo</code>, which it doesn't seem possible to make a <code>Functor</code> instance out of.</p>
  
  <p>But without that, I'm drawing a blank as to how to deal with this, since the LHS of a combinator like <code>&gt;=.</code> has to be an <code>EntityField</code> value, which stops me from trying to apply functions to the database value before comparing.</p>
  
  <p>Since I know someone is going to say it (and most of you will be thinking it) - yes, in this toy example I could just as easily make the <code>a</code> and <code>b</code> into separate fields in my database table, and solve the problem that way. As I said, this is somewhat simplified, and in my real application doing it that way would feel unsatisfactory for a number of reasons. And it doesn't solve my wider question of, essentially, how to be able to do arbitrary transformations on the data before querying. [Edit: I have now gone with this approach, in order to move forward with my project, but as I said it's not totally satisfactory, and I'm still waiting for an answer to my general question - even if that's just "sorry it's not possible", as I increasingly suspect, I would appreciate a good explanation.]</p>
  
  <p>I'm beginning to suspect this may simply be impossible because the data is ultimately stored in an SQL database and SQL simply isn't as expressive as Haskell - but what I'm trying to do, at least with record types (I confess I don't know how <code>derivePersistField</code> marshals these to SQL data types) doesn't seem too unreasonable so I feel I should ask if there are any workarounds for this, or do I really have to decompose my records into a bunch of separate fields if I want to query them individually.</p>
  
  <p>[If there are any other libraries which can help then feel free to recommend them - I did look into Esqueleto but decided I didn't need it for this project, although that was before I ran into this problem. Would that be something that could help with this kind of query?]</p>