【问题标题】:How to use record-syntax with Yesod-Persistent如何在 Yesod-Persistent 中使用记录语法
【发布时间】:2016-05-15 16:09:09
【问题描述】:

为什么以下操作不起作用?我有点知道引擎盖下有很多东西,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’

【问题讨论】:

    标签: haskell yesod


    【解决方案1】:

    函数名称类似于:userEmailuserCreatedAtuserUpdatedAt

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

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 2020-03-17
      • 1970-01-01
      • 2012-06-14
      • 2012-07-28
      • 1970-01-01
      相关资源
      最近更新 更多