我正在尝试使用Yesod的Persistent模块来为我的网站构建数据库(使用Yesod在Haskell中完成)。这是我的模型文件:

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


首次编译时,出现以下错误:

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"))


然后,在.cabal文件的time部分中添加了build-depends模块。这消除了最后一个错误,但是我现在遇到以下错误:

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'


对于第一个错误,据我了解(即我对yesod book的唯一性约束部分的了解),如果我想使字段唯一,则只需在表定义的末尾添加一行字符串“ Unique”带有空格,然后是我要唯一的字段名称。我错了吗?

至于最后两个错误,我没有在任何地方声明这些字段,所以我不知道它们为什么在那里。有什么见解吗?

最佳答案

以下内容在persistent-1.3.1.1, persistent-mongoDB-1.4.1, persistent-template 1.3.1.4上为我编译。我列出了实用程序和模块,以防它们成为问题的根源。

{-# 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
|]

09-25 15:31