我有一张带有可能外键的表。我正在尝试加入,但无法编译。
CatTable
name Text
MyTable
category CatTableId Maybe
amount Double
我的查询:
myQuery :: (PersistQuery (SqlPersistT m), MonadLogger m , MonadResourceBase m) =>
SqlPersistT m [(E.Value (Maybe (Text)), E.Value (Maybe Double))]
myQuery = do
E.select $ E.from $ \(t `E.LeftOuterJoin` c) -> do
E.on (t E.?. MyTableCategory E.==. E.just (c E.^. CatTableId))
E.groupBy $ E.just (c E.^. CatTableName)
let sum' = E.sum_ (t E.^. MyTableAmount)
E.orderBy [E.desc sum']
return (E.just (c E.^. CatTableName) , sum' )
我收到这种类型的错误:
Couldn't match type `KeyBackend SqlBackend CatTable'
with `Maybe (KeyBackend SqlBackend CatTable)'
Expected type: EntityField
CatTable (Maybe (KeyBackend SqlBackend CatTable))
Actual type: EntityField
CatTable (KeyBackend SqlBackend CatTable)
In the second argument of `(^.)', namely `CatTableId'
In the first argument of `just', namely `(c ^. CatTableId)'
In the second argument of `(E.==.)', namely
`just (c ^. CatTableId)'
Couldn't match type `Maybe (Entity MyTable)' with `Entity MyTable'
Expected type: SqlExpr (Entity MyTable)
Actual type: SqlExpr (Maybe (Entity MyTable))
In the first argument of `(^.)', namely `t'
In the first argument of `sum_', namely `(t ^. MyTableAmount)'
In the expression: sum_ (t ^. MyTableAmount)
我尝试了不同的组合,^.
但?.
没有运气。也尝试删除或添加just
。在这一点上,我只是在猜测,不了解如何解决该错误。因此,感谢有关如何加入 Maybe 领域的任何意见。我假设这groupBy
可能会使它复杂化,因为它不是 CatTable 中的 Maybe,但一旦加入它就会是。
select * from cat_table;
id|name
1|A
2|B
3|C
select * from my_table;
id|category|amount
1|1|55.0
2|1|15.0
3|2|10.0
4|2|60.0
5||60.0
select name, sum(amount) from my_table as m left join cat_table as c
on m.category = c.id
group by name;
|60.0
A|70.0
B|70.0