如何使用Yesod-Persistent记录语法

Sau*_*nda 5 haskell yesod

以下为什么不工作?我有点知道,有很多去引擎盖下和User类型可能不真的有email,createdAtupdatedAt领域.什么是不使用位置参数实例化对象的最佳方法,这很容易失控?

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
  email String
  createdAt UTCTime Maybe default=CURRENT_TIME
  updatedAt UTCTime Maybe default=CURRENT_TIME
  deriving Show
]]

main :: IO ()
main = runSqlite ":memory:" $ do
  runMigration migrateAll
  u <- insert $ User {email="saurabhnanda@gmail.com" createdAt=Nothing updatedAt=Nothing}
Run Code Online (Sandbox Code Playgroud)

编译错误:

trysql.hs:38:23:
    ‘email’ is not a (visible) field of constructor ‘User’

trysql.hs:38:55:
    ‘createdAt’ is not a (visible) field of constructor ‘User’

trysql.hs:38:74:
    ‘updatedAt’ is not a (visible) field of constructor ‘User’
Run Code Online (Sandbox Code Playgroud)

小智 6

该功能的名称会是这样:userEmail,userCreatedAt,userUpdatedAt.

运行cabal REPL并浏览项目有助于识别Yesod为持久类型生成的函数.