简体   繁体   中英

What is the ideal way to select rows by projected keys in Groundhog Haskell

I'm trying to create a many to many table using Groundhog in Haskell, which basically looks like this if I cut away all the other logic I have:

data FooRow = FooRow {
  fooRowUUID :: UUID
}
deriving instance Show FooRow

data BarRow = BarRow {
  barRowUUID :: UUID
}
deriving instance Show BarRow

data FooToBarRow = FooToBarRow {
  fooToBarRowUUID :: UUID,
  fooToBarRowFoo :: DefaultKey FooRow,
  fooToBarRowBar :: DefaultKey BarRow
}
deriving instance Show FooToBarRow

Now, trying to define operations, I can get and insert all of these records just fine, however I'm not sure how to go from having a FooRow, with it's ID, and then get all the related BarRows by way of the many to many table. Right now I've played with something like this:

getBarsForFoo fooID = do
  barKeys <- project 
    (FooToBarRowBarField)
    (FooToBarRowFooField ==. (Foo_FooKey fooID))
  select $ (BarRowUUIDField `in_` barKeys)

However this doesn't typecheck, with the error:

Couldn't match type 'UUID' with 'BarRow'

Inspecting just the results of the project with putStrLn, I can see that the type of barKeys is:

[Bar_BarKey UUID]

but I don't quite understand how to make use of that within my query. I don't see any examples like this in the Groundhog documentation, so I'm hoping someone will be able to put me on the right path here.

I'm quite certain there are more efficient ways to go about this (there's going to be a bunch of underlying queries with this approach), but this does at at least get the job done for now.

getBarsForFoo fooID = do
    barKeys <- project 
        (FooToBarRowBarField)
        (FooToBarRowFooField ==. (Foo_FooKey fooID))
    q <- mapM (getBy) barKeys
    return (catMaybes q :: [BarRow])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM