Yesod、Persistent 和 MySQL 没有实例错误
No Instance Error with Yesod, Persistent and MySQL
我不确定要在此 post 中包含什么,因为我真的不理解错误消息,所以我只包含了错误、Model.hs 文件和模型。我正在为 MySQL 使用 Yesod 脚手架网站。我使用的操作系统是Ubuntu。如果您需要查看任何其他代码,例如数据定义然后问。
错误信息:
[ 6 of 20] Compiling Model ( src/Model.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/Model.o )
/home/james/ConVoke/convoke-website/src/Model.hs:24:7: error:
• No instance for (persistent-2.7.0:Database.Persist.Sql.Class.PersistFieldSql
Language)
arising from a use of ‘persistent-2.7.0:Database.Persist.Sql.Class.sqlType’
• In the fourth argument of ‘FieldDef’, namely
‘persistent-2.7.0:Database.Persist.Sql.Class.sqlType
(Data.Proxy.Proxy :: Data.Proxy.Proxy Language)’
In the expression:
FieldDef
(HaskellName (packPTH "language"))
(DBName (packPTH "language"))
(FTTypeCon Nothing (packPTH "Language"))
(persistent-2.7.0:Database.Persist.Sql.Class.sqlType
(Data.Proxy.Proxy :: Data.Proxy.Proxy Language))
[]
True
NoReference
In the ‘entityFields’ field of a record
-- While building package website-0.0.0 using:
/home/james/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.24.2.0 build lib:website --ghc-options " -ddump-hi -ddump-to-file"
Model.hs:
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Model where
import ClassyPrelude.Yesod
import Database.Persist.Quasi
import Import.Game
import Import.Language
import Import.Profile
-- You can define all of your database entities in the entities file.
-- You can find more information on persistent and how to declare entities
-- at:
-- http://www.yesodweb.com/book/persistent/
share [mkPersist sqlSettings, mkMigrate "migrateAll"]
$(persistFileWith lowerCaseSettings "config/models")
型号:
Player
name Text
username Text
dob Day
language Language
tournaments [TournamentResult]
deriving (Show)
PlatformAccount
player PlayerId
platform Platform
account Text
deriving (Show)
SocialAccount
player PlayerId
site SocialSite
account Text
deriving (Show)
PastTeam
player PlayerId
team TeamId
yearJoined Int
yearLeft Int
deriving (Show)
TournamentResult
player PlayerId
team TeamId
name Text
placing Text
year Int
deriving (Show)
Roster
name Text
game Game
team TeamId
players [PlayerId]
creationDay Day
deriving (Show)
PlayerRole
roster RosterId
player PlayerId
role Role
deriving (Show)
CompetitiveGame
game Game
platform Platform
role Role
availability Availability
playingSince Int
deriving (Show)
Team
name Text
creationDay Day
deriving (Show)
提前致谢:)
编辑:根据要求,定义语言数据类型的 Language.hs 文件:
module Import.Language where
allLanguages :: [Language]
allLanguages = [Afrikanns ..]
data Language = Afrikanns |
Albanian |
Arabic |
Armenian |
Basque |
Bengali |
Bulgarian |
Catalan |
Cambodian |
Chinese_Mandarin |
Croation |
Czech |
Danish |
Dutch |
English |
Estonian |
Fiji |
Finnish |
French |
Georgian |
German |
Greek |
Gujarati |
Hebrew |
Hindi |
Hungarian |
Icelandic |
Indonesian |
Irish |
Italian |
Japanese |
Javanese |
Korean |
Latin |
Latvian |
Lithuanian |
Macedonian |
Malay |
Malayalam |
Maltese |
Maori |
Marathi |
Mongolian |
Nepali |
Norwegian |
Persian |
Polish |
Portuguese |
Punjabi |
Quechua |
Romanian |
Russian |
Samoan |
Serbian |
Slovak |
Slovenian |
Spanish |
Swahili |
Swedish |
Tamil |
Tatar |
Telugu |
Thai |
Tibetan |
Tonga |
Turkish |
Ukranian |
Urdu |
Uzbek |
Vietnamese |
Welsh |
Xhosa
deriving (Enum, Show, Eq)
该错误表明它无法找到与 sum 类型相关的 Persistent 实例。你必须做两件事来修复它:
=> 确保为您的求和类型派生 Show
和 Read
实例。示例:
data Language = JS | Haskell deriving (Eq, Show, Read, Ord)
=> 使用模板 haskell:
为其导出持久化相关实例
derivePersistField "Language"
您必须对您用于模型的所有求和类型执行此操作。
我不确定要在此 post 中包含什么,因为我真的不理解错误消息,所以我只包含了错误、Model.hs 文件和模型。我正在为 MySQL 使用 Yesod 脚手架网站。我使用的操作系统是Ubuntu。如果您需要查看任何其他代码,例如数据定义然后问。
错误信息:
[ 6 of 20] Compiling Model ( src/Model.hs, .stack-work/dist/x86_64-linux/Cabal-1.24.2.0/build/Model.o )
/home/james/ConVoke/convoke-website/src/Model.hs:24:7: error:
• No instance for (persistent-2.7.0:Database.Persist.Sql.Class.PersistFieldSql
Language)
arising from a use of ‘persistent-2.7.0:Database.Persist.Sql.Class.sqlType’
• In the fourth argument of ‘FieldDef’, namely
‘persistent-2.7.0:Database.Persist.Sql.Class.sqlType
(Data.Proxy.Proxy :: Data.Proxy.Proxy Language)’
In the expression:
FieldDef
(HaskellName (packPTH "language"))
(DBName (packPTH "language"))
(FTTypeCon Nothing (packPTH "Language"))
(persistent-2.7.0:Database.Persist.Sql.Class.sqlType
(Data.Proxy.Proxy :: Data.Proxy.Proxy Language))
[]
True
NoReference
In the ‘entityFields’ field of a record
-- While building package website-0.0.0 using:
/home/james/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.24.2.0 build lib:website --ghc-options " -ddump-hi -ddump-to-file"
Model.hs:
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Model where
import ClassyPrelude.Yesod
import Database.Persist.Quasi
import Import.Game
import Import.Language
import Import.Profile
-- You can define all of your database entities in the entities file.
-- You can find more information on persistent and how to declare entities
-- at:
-- http://www.yesodweb.com/book/persistent/
share [mkPersist sqlSettings, mkMigrate "migrateAll"]
$(persistFileWith lowerCaseSettings "config/models")
型号:
Player
name Text
username Text
dob Day
language Language
tournaments [TournamentResult]
deriving (Show)
PlatformAccount
player PlayerId
platform Platform
account Text
deriving (Show)
SocialAccount
player PlayerId
site SocialSite
account Text
deriving (Show)
PastTeam
player PlayerId
team TeamId
yearJoined Int
yearLeft Int
deriving (Show)
TournamentResult
player PlayerId
team TeamId
name Text
placing Text
year Int
deriving (Show)
Roster
name Text
game Game
team TeamId
players [PlayerId]
creationDay Day
deriving (Show)
PlayerRole
roster RosterId
player PlayerId
role Role
deriving (Show)
CompetitiveGame
game Game
platform Platform
role Role
availability Availability
playingSince Int
deriving (Show)
Team
name Text
creationDay Day
deriving (Show)
提前致谢:)
编辑:根据要求,定义语言数据类型的 Language.hs 文件:
module Import.Language where
allLanguages :: [Language]
allLanguages = [Afrikanns ..]
data Language = Afrikanns |
Albanian |
Arabic |
Armenian |
Basque |
Bengali |
Bulgarian |
Catalan |
Cambodian |
Chinese_Mandarin |
Croation |
Czech |
Danish |
Dutch |
English |
Estonian |
Fiji |
Finnish |
French |
Georgian |
German |
Greek |
Gujarati |
Hebrew |
Hindi |
Hungarian |
Icelandic |
Indonesian |
Irish |
Italian |
Japanese |
Javanese |
Korean |
Latin |
Latvian |
Lithuanian |
Macedonian |
Malay |
Malayalam |
Maltese |
Maori |
Marathi |
Mongolian |
Nepali |
Norwegian |
Persian |
Polish |
Portuguese |
Punjabi |
Quechua |
Romanian |
Russian |
Samoan |
Serbian |
Slovak |
Slovenian |
Spanish |
Swahili |
Swedish |
Tamil |
Tatar |
Telugu |
Thai |
Tibetan |
Tonga |
Turkish |
Ukranian |
Urdu |
Uzbek |
Vietnamese |
Welsh |
Xhosa
deriving (Enum, Show, Eq)
该错误表明它无法找到与 sum 类型相关的 Persistent 实例。你必须做两件事来修复它:
=> 确保为您的求和类型派生 Show
和 Read
实例。示例:
data Language = JS | Haskell deriving (Eq, Show, Read, Ord)
=> 使用模板 haskell:
为其导出持久化相关实例derivePersistField "Language"
您必须对您用于模型的所有求和类型执行此操作。