如何使用Yesod-Persistent记录语法

时间:2016-02-05 15:53:17

标签: haskell yesod

为什么以下不起作用?我知道有很多内容,User类型可能没有真正的emailcreatedAtupdatedAt字段。什么是不使用位置参数实例化对象的最佳方法,这很容易失控?

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’

1 个答案:

答案 0 :(得分:6)

函数名称如下:userEmailuserCreatedAtuserUpdatedAt

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