prowdsponsor / esqueleto

Bare bones, type-safe EDSL for SQL queries on persistent backends.
http://hackage.haskell.org/package/esqueleto
BSD 3-Clause "New" or "Revised" License
177 stars 51 forks source link

`valkey` not working: `No instance for (ToBackendKey SqlBackend Product)` #161

Open MuhammedZakir opened 2 years ago

MuhammedZakir commented 2 years ago

Schema:

share [ mkPersist sqlSettings
      , mkMigrate "migrateAll" ] [persistUpperCase|
Product
  Id    Int
  name  Text
  deriving Eq Show
]

Helper functions:

dbName = "/tmp/esqueleto.db"

connectionPool = do
  conInfo <- walEnabled (const $ pure False) $ mkSqliteConnectionInfo dbName
  runStdoutLoggingT $ createSqlitePoolFromInfo conInfo 1

pool = connectionPool

q q = pool >>= \p -> runSqlPool q p

For above schema, running this in ghci

q $ select $ (from $ table @Product) >>= \ps -> where_ (ps ^. ProductId ==. valkey 1) >> pure ps

throws

• No instance for (ToBackendKey SqlBackend Product)
        arising from a use of ‘valkey’

<context info>

This works:

q $ select $ (from $ table @Product) >>= \ps -> where_ (ps ^. ProductId ==. (val $ ProductKey 1)) >> pure ps
MuhammedZakir commented 2 years ago

Is ProductId treated differently? This works:

import Database.Persist as DP ( (==.) )

q $ deleteWhere [ProductName DP.==. "some name"]

This doesn't:

q $ deleteWhere [ProductId DP.==. 1]

throws

• No instance for (Num ProductId) arising from the literal ‘15’
• In the second argument of ‘(DP.>=.)’, namely ‘15’

Note: I ran these in ghci.

MuhammedZakir commented 2 years ago

Out of topic: [ProductName ==. "text"] is better than[ps ^. ProductName ==. val "name"]`. For the query, I can understand it as maybe optimization, better generated query and better type-level safety. But for this, I don't understand. What was the reason for choosing the latter?


Late, but, thank you very much for this library!