Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use record-syntax with Yesod-Persistent

Tags:

haskell

yesod

Why doesn't the following work? I kinda know that there's a lot going under the hood and the User type probably doesn't really have the email, createdAt, and updatedAt field. What's the best way to NOT instantiate objects using positional parameters, which can easily go out of hand?

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="[email protected]" createdAt=Nothing updatedAt=Nothing}

Compilation errors:

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’
like image 391
Saurabh Nanda Avatar asked Feb 05 '16 15:02

Saurabh Nanda


1 Answers

The function names would be like: userEmail, userCreatedAt, userUpdatedAt.

Running cabal REPL and browsing the project help identify Yesod generated functions for persistent types.

like image 78
dganti Avatar answered Sep 28 '22 01:09

dganti