如何在 Yesod-Persistent 中使用记录语法
How to use record-syntax with Yesod-Persistent
为什么以下不起作用?我知道幕后有很多事情要做,User
类型可能实际上没有 email
、createdAt
和 updatedAt
字段。不使用位置参数实例化对象的最佳方法是什么,这很容易失控?
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}
编译错误:
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’
函数名称如下:userEmail
、userCreatedAt
、userUpdatedAt
。
运行 cabal REPL 和浏览项目有助于识别 Yesod 为持久类型生成的函数。
为什么以下不起作用?我知道幕后有很多事情要做,User
类型可能实际上没有 email
、createdAt
和 updatedAt
字段。不使用位置参数实例化对象的最佳方法是什么,这很容易失控?
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}
编译错误:
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’
函数名称如下:userEmail
、userCreatedAt
、userUpdatedAt
。
运行 cabal REPL 和浏览项目有助于识别 Yesod 为持久类型生成的函数。