Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sum_ and friends can cause runtime type errors #148

Open
enolan opened this issue Aug 29, 2016 · 0 comments
Open

sum_ and friends can cause runtime type errors #148

enolan opened this issue Aug 29, 2016 · 0 comments

Comments

@enolan
Copy link

enolan commented Aug 29, 2016

sum_'s type is (PersistField a, PersistField b) => expr (Value a) -> expr (Value (Maybe b)). But sum of course cannot return any type. In Postgres, it returns numeric for bigints, which is represented as PersistRational. Details here. Example:

module Main where

import Control.Monad.IO.Class
import Control.Monad.Logger
import Database.Esqueleto
import Database.Persist
import Database.Persist.Postgresql
import Database.Persist.TH

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Number
   value Int
|]

connStr = "host=localhost dbname=test user=test password=test port=5432"

main :: IO ()
main = runStderrLoggingT $ withPostgresqlPool connStr 10 $ \pool -> liftIO $ do
    flip runSqlPersistMPool pool $ do
        runMigration migrateAll

        johnId <- insert $ Number 5
        janeId <- insert $ Number 22
        return ()
        _ :: [Value (Maybe Int)] <- select $
          from $ \number -> do
            return $ sum_ $ number ^. NumberValue
        return ()
enolan at behemoth in ~/junk/esqueleto-bug  
$ stack exec esqueleto-bug
[Debug#SQL] INSERT INTO "number"("value") VALUES(?) RETURNING "id"; [PersistInt64 5]
[Debug#SQL] INSERT INTO "number"("value") VALUES(?) RETURNING "id"; [PersistInt64 22]
[Debug#SQL] SELECT SUM("number"."value")
FROM "number"
; []
esqueleto-bug: PersistMarshalError "int Expected Integer, received: PersistRational (27 % 1)"

I suppose it needs a type family/fundep. Test case repo here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant