haskell

persistentのエラー:Unknown column in unique constraint

Haskellのpersistentを使っていたら、こんなエラーが出てハマった。

Exception when trying to run compile-time code:\n    Unknown column in unique constraint: \"Double\" [FieldDef {fieldHaskell = HaskellName {unHaskellName = \"name\"}, fieldDB = DBName {unDBName = \"name\"}, fieldType = FTTypeCon Nothing \"String\", fieldSqlType = SqlOther \"SqlType unset for name\", fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = \"tickerSymbol\"}, fieldDB = DBName {unDBName = \"ticker_symbol\"}, fieldType = FTTypeCon Nothing \"String\", fieldSqlType = SqlOther \"SqlType unset for tickerSymbol\", fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = \"closePrice\"}, fieldDB = DBName {unDBName = \"close_price\"}, fieldType = FTTypeCon Nothing \"Double\", fieldSqlType = SqlOther \"SqlType unset for closePrice\", fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = \"hightPrice\"}, fieldDB = DBName {unDBName = \"hight_price\"}, fieldType = FTTypeCon Nothing \"Double\", fieldSqlType = SqlOther \"SqlType unset for hightPrice\", fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = \"lowPrice\"}, fieldDB = DBName {unDBName = \"low_price\"}, fieldType = FTTypeCon Nothing \"Double\", fieldSqlType = SqlOther \"SqlType unset for lowPrice\", fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = \"priceDate\"}, fieldDB = DBName {unDBName = \"price_date\"}, fieldType = FTTypeCon Nothing \"Day\", fieldSqlType = SqlOther \"SqlType unset for priceDate\", fieldAttrs = [], fieldStrict = True, fieldReference = NoReference},FieldDef {fieldHaskell = HaskellName {unHaskellName = \"createdAt\"}, fieldDB = DBName {unDBName = \"created_at\"}, fieldType = FTTypeCon Nothing \"UTCTime\", fieldSqlType = SqlOther \"SqlType unset for createdAt\", fieldAttrs = [\"default=CURRENT_TIMESTAMP\"], fieldStrict = True, fieldReference = NoReference}]\"OpenPrice\" []\nCallStack (from HasCallStack):\n  error, called at ./Database/Persist/Quasi.hs:505:7 in persistent-2.9.2-169pfjG8FKk3OF5NT7ZNUi:Database.Persist.Quasi\n  Code: template-haskell-2.14.0.0:Language.Haskell.TH.Quote.quoteExp\n          persistLowerCase\n          \"\\n\\\n          \\Stock json\\n\\\n          \\    name String\\n\\\n          \\    tickerSymbol String\\n\\\n          \\    OpenPrice Double\\n\\\n          \\    closePrice Double\\n\\\n          \\    hightPrice Double\\n\\\n          \\    lowPrice Double\\n\\\n          \\    priceDate Day\\n\\\n          \\    createdAt UTCTime default=CURRENT_TIMESTAMP\\n\\\n          \\    deriving Read Eq Generic Show\\n\"\n• In the quasi-quotation:\n    [persistLowerCase|\nStock json\n    name String\n    tickerSymbol String\n    OpenPrice Double\n    closePrice Double\n    hightPrice Double\n    lowPrice Double\n    priceDate Day\n    createdAt UTCTime default=CURRENT_TIMESTAMP\n    deriving Read Eq Generic Show\n|]

Unknown column in unique constraint: \"Double\"というメッセージが出ていたので、もしかしてDoubleがサポートされていないのかと思って調べたが、ちゃんとサポートされている。全然原因がわからなくて色々試行錯誤しているうちに、カラム名の最初の文字を大文字にしてしまっていたことに気づいた。確かに、Unknow columnと言っているので、Doubleに引っ張られず素直にカラム名に問題があると考えればよかったのだが、気づくまでしばらくかかってしまった。そしてこのエラーはググってもpersistentの実装のgithubしかヒットしなかった。今後同じエラーにハマった人のために。

ダメなやつ。OpenPriceが大文字から始まっている。Oを小文字に変えればOK。

share
  [mkPersist sqlSettings, mkMigrate "migrateAll"]
  [persistLowerCase|
Stock json
    name String
    tickerSymbol String
    OpenPrice Double
    closePrice Double
    hightPrice Double
    lowPrice Double
    priceDate Day
    createdAt UTCTime default=CURRENT_TIMESTAMP
    deriving Read Eq Generic Show
|]

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です