简体   繁体   English

在Groundhog Haskell中通过投影键选择行的理想方法是什么

[英]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: 我正在尝试使用Haskell中的Groundhog创建一个多对多的表,如果我切除了我拥有的所有其他逻辑,它基本上看起来像这样:

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. 现在,尝试定义操作,我可以很好地获取并插入所有这些记录,但是我不知道如何从拥有它的ID的FooRow,然后通过许多来获得所有相关的BarRows很多桌子。 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: 使用putStrLn检查项目的结果,我可以看到barKeys的类型是:

[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. 我没有在Groundhog文档中看到这样的例子,所以我希望有人能够把我放在正确的道路上。

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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