Reputation: 1163
I'm trying to use Yesod's Persistent module in order to build my database for my website (done in Haskell with Yesod). Here is my models file:
User
idAccount AccountId Int
userLastName Text Maybe
userFirstName Text Maybe
userAge Int Maybe
userSex Text Maybe
userEMail Text
UniqueUserEMail userEMail
Account
accountName Text
accountPassword Text
accountCreatedDate UTCTime default=CURRENT_TIME
accountLastLogin UTCTime default=CURRENT_TIME
UniqueAccountName accountName
When I first compiled, I got the following error:
Model.hs:14:7:
Not in scope: type constructor or class `UTCTime'
In the result of the splice:
$(persistFileWith lowerCaseSettings "config/models")
To see what the splice expanded to, use -ddump-splices
In the second argument of `share', namely
`$(persistFileWith lowerCaseSettings "config/models")'
In the expression:
share
[mkPersist sqlOnlySettings, mkMigrate "migrateAll"]
($(persistFileWith lowerCaseSettings "config/models"))
I then added the time
module in my build-depends
section in my .cabal file. This removed the last error, but I now have the following errors:
Foundation.hs:135:22:
Not in scope: data constructor `UniqueUser'
Perhaps you meant `UniqueDef' (imported from Yesod)
Foundation.hs:140:23:
`userIdent' is not a (visible) field of constructor `User'
Foundation.hs:141:23:
`userPassword' is not a (visible) field of constructor `User'
For the first error, to my understanding (I.E., what I understand of the uniqueness constraint section of the yesod book), if I want to make a field unique, I just have to add a line at the end of the table definition starting with the string "Unique" with a space and then the name of the field that I want to be unique. Am I mistaken?
As for the last two errors, I do not have those fields declared anywhere, so I do not know why they are there. Any insights on this?
Upvotes: 0
Views: 472
Reputation: 1163
I have found the source of my errors. Since I am using the scaffold site, there is already some stuff implemented for me. One of those things is the Yesod Auth module which was tied with the default Persistent module database structure. So when I changed the database structure in my models
file, it was not compatible anymore with the default Auth module code in the Foundation.hs
file, namely the getAuthId function:
getAuthId creds = runDB $ do
x <- getBy $ UniqueUser $ credsIdent creds
case x of
Just (Entity uid _) -> return $ Just uid
Nothing -> do
fmap Just $ insert User
( userIdent = credsIdent creds
, userPassword = Nothing
)
I do not know how to modify this code to fit my purposes yet, so I will update this answer once I do.
Upvotes: 0
Reputation: 4166
The following compiles for me on persistent-1.3.1.1, persistent-mongoDB-1.4.1, persistent-template 1.3.1.4
. I've listed out the pragmas and the modules in case they are a source of the problem.
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveGeneric #-}
import Database.Persist
import Database.Persist.TH
import Database.Persist.MongoDB
import Language.Haskell.TH.Syntax
import Data.Time.Clock.POSIX (getPOSIXTime,posixSecondsToUTCTime)
import Data.Text (Text)
import Data.Time (UTCTime,TimeOfDay)
let mongoSettings = (mkPersistSettings (ConT ''MongoBackend)) {mpsGeneric = False}
in share [mkPersist mongoSettings] [persistLowerCase|
User
idAccount AccountId Int
userLastName Text Maybe
userFirstName Text Maybe
userAge Int Maybe
userSex Text Maybe
userEMail Text
UniqueUserEMail userEMail
Account
accountName Text
accountPassword Text
accountCreatedDate UTCTime default=CURRENT_TIME
accountLastLogin UTCTime default=CURRENT_TIME
UniqueAccountName accountName
|]
Upvotes: 1