标签: yesod

结合持久性和IO

我正试图通过目录递归,处理文件并将结果存储在数据库中,但我遇到了问题.

我正在尝试做的简化示例如下:

{-# LANGUAGE QuasiQuotes, TemplateHaskell, TypeFamilies, OverloadedStrings #-}
{-# LANGUAGE GADTs, FlexibleContexts #-}
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH
import System.Environment (getArgs)
import System.Directory (canonicalizePath, getDirectoryContents, doesDirectoryExist, doesFileExist)
import System.FilePath (combine, takeExtension)
import Control.Monad (filterM, mapM_)
import Control.Monad.IO.Class (liftIO)

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistUpperCase|
File                                                       
  path String
  deriving (Show)
|]
main :: IO ()
main = do
  args <- getArgs
  path <- canonicalizePath $ head args
  runSqlite "files.sqlite" $ do
    runMigration migrateAll
    liftIO $ processDirectory path
    return () …
Run Code Online (Sandbox Code Playgroud)

haskell yesod

2
推荐指数
1
解决办法
329
查看次数

在Ubuntu 13.10上设置Yesod

按照QuickStart上的说明操作后,我在启动网页时看到了Yesod中的一些错误.错误以:

...

不能满足-package-id wai-extra-1.3.4.6-62543d69d10941dae1d9b206c3eb3067:

wai-extra-1.3.4.6-62543d69d10941dae1d9b206c3eb3067 is unusable due to missing or recursive dependencies:
  blaze-builder-conduit-1.0.0-1053545317cd68e3d51439dd9a0e622d zlib-conduit-1.0.0-b51dc7daf506ea4c5ecd031c5101d96a
(use -v for more information)
Run Code Online (Sandbox Code Playgroud)

构建失败,暂停......

希望相关,参考教程让我运行

cabal-dev安装

在一个新的Yesod项目上,但是这也给了我错误:

...

[1/1]编译Control.Monad.Logger(Control/Monad/Logger.hs,dist/build/Control/Monad/Logger.o)

Control/Monad/Logger.hs:63:39:模块System.Log.FastLogger' does not exportpushLogStr'

Control/Monad/Logger.hs:63:72:模块System.Log.FastLogger' does not export记录器集'

Control/Monad/Logger.hs:63:83:模块System.Log.FastLogger' does not exportnewLoggerSet'

Control/Monad/Logger.hs:63:97:模块System.Log.FastLogger' does not exportdefaultBufSize'无法安装monad-logger-0.3.3.0

...

在后来的这组错误中,我遇到了一个页面,表明该问题已得到修复(因此不会让我发布超过2个链接,但Google会出现错误,并且会出现问题).

有任何想法吗?

提前致谢!

yesod cabal-dev ubuntu-13.10

2
推荐指数
1
解决办法
1623
查看次数

如何在Esqueleto查询中使用分组依据和总和

我正在尝试使用Esqueleto的示例查询之一,但无法对其进行编译。唯一的变化是我在没有联接的情况下使用它。

我有一个看起来像这样的表:

sqlite> select * from my_table;
id|category|amount
1|A|1.0
2|A|2.0
3|B|2.0
4|B|8.0
Run Code Online (Sandbox Code Playgroud)

我想这样做:

select category,sum(amount) from my_table group by category;
category|sum(amount)
A|3.0
B|10.0
Run Code Online (Sandbox Code Playgroud)

这是我的查询:

import qualified Database.Esqueleto as E

r <- runDB $
    E.select $ E.from $ \t  -> do
        E.groupBy $ t E.^. MyTableCategory
        let sum' = E.sum_ (t E.^. MyTableAmount)
        E.orderBy [E.desc sum']
        return (t E.^. MyTableCategory, sum' )
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

No instance for (PersistField b0) arising from a use of `E.select'
The type variable `b0' is ambiguous
Possible …
Run Code Online (Sandbox Code Playgroud)

haskell yesod esqueleto

2
推荐指数
1
解决办法
712
查看次数

如何捕获httpLbs(http-client)引发的异常

我有一个使用http-client https://www.stackage.org/haddock/lts-9.0/http-client-0.5.7.0/Network-HTTP-Client.html#v:httpLbs的yesod应用程序。我在打电话

resp <- httpLbs req man
Run Code Online (Sandbox Code Playgroud)

Handler (Response BSL.ByteString) 单子里面。

我得到这个(在应用程序的日志中)

6/Aug/2017:15:14:17 +0200 [Error#yesod-core] HttpExceptionRequest Request { ...
Run Code Online (Sandbox Code Playgroud)

(下一行代码永远不会执行)

相反,我想捕获异常,并在我的代码中对其进行处理。怎么样?

haskell yesod

2
推荐指数
1
解决办法
154
查看次数

用于自定义主键的持久性 `toSqlKey`

我定义如下模型,其中Category有一个非整数的主键code类型Text

Category
    code Text
    Primary code
    deriving Show Typeable

CategoryTreeNode
    name Text
    code CategoryId Maybe
    lft Int MigrationOnly default=0
    rgt Int MigrationOnly default=0
    deriving Show Typeable
Run Code Online (Sandbox Code Playgroud)

对于没有定义的模型Primary,我可以使用toSqlKey将任意Int64s 转换为Key Category. 具有自定义主键的模型是否有等效功能?

persistent yesod

2
推荐指数
1
解决办法
275
查看次数

Yesod 中模板 haskell 的评估

在浏览 Yesod Book 的示例时,我遇到了以下代码片段的问题:

\n
{-# LANGUAGE OverloadedStrings #-}\n{-# LANGUAGE QuasiQuotes       #-}\n{-# LANGUAGE TemplateHaskell   #-}\n{-# LANGUAGE TypeFamilies      #-}\n{-# LANGUAGE ViewPatterns      #-}\nimport           Data.Text (Text)\nimport qualified Data.Text as T\nimport           Yesod\n\ndata App = App\ninstance Yesod App\n\nmkYesod "App" [parseRoutes|\n/person/#Text PersonR GET\n/year/#Integer/month/#Text/day/#Int DateR\n/wiki/*Texts WikiR GET\n|]\n\ngetPersonR :: Text -> Handler Html\ngetPersonR name = defaultLayout [whamlet|<h1>Hello #{name}!|]\n\nhandleDateR :: Integer -> Text -> Int -> Handler Text -- text/plain\nhandleDateR year month day =\n    return $\n        T.concat [month, " ", T.pack $ show day, ", ", T.pack $ show …
Run Code Online (Sandbox Code Playgroud)

haskell yesod template-haskell

2
推荐指数
1
解决办法
163
查看次数

有人可以向我解释我应该如何修复这种类型的签名?

这是代码,我试着让类型推断找出函数的类型.代码编译时,它在运行时失败.

Ambiguous type variables `b0', `m0' in the constraint:
  (PersistBackend b0 m0) arising from a use of `isFree'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: isFree testDay
In an equation for `it': it = isFree testDay
Run Code Online (Sandbox Code Playgroud)

:t isFree

isFree :: PersistBackend b m => C.Day -> b m Bool

>isFree day = do
   match <- selectList [TestStartDate ==. day,
                        TestStatus !=. Passed,
                        TestStatus !=. Failed] []
   if (L.null match) then (liftIO $ return …
Run Code Online (Sandbox Code Playgroud)

haskell types persistent yesod

1
推荐指数
1
解决办法
128
查看次数

带有IO String的setTitle会导致类型错误

我正在尝试将网页的标题设置为包含当前年份的字符串,如下所示:

getCurrentYear :: IO String
getCurrentYear = do
    now <- getCurrentTime
    let today = utctDay now
    let (year, _, _) = toGregorian today
    return $ show year

title :: IO Html
title = do
    y <- getCurrentYear
    return $ toHtml $ "Registration " ++ y

getRootR :: Handler RepHtml
getRootR = do
    (widget, enctype) <- generateFormPost personForm -- not important for the problem at hand, comes from the example in the yesod book
    defaultLayout $ do
        setTitle title -- this …
Run Code Online (Sandbox Code Playgroud)

haskell types yesod

1
推荐指数
1
解决办法
169
查看次数

如何将XyzId转换为持久性中的整数?

在Persistent中,如何将XyzId(PersonId,whateverid)转换为其表示的整数?

haskell persistent yesod

1
推荐指数
1
解决办法
127
查看次数

Yesod:被困在"七周内的七个Web框架"一书中

我根据快速入门指南安装了Yesod .

我在七周内购买了"七个Web框架"这本书,我正在努力跟进.我在第204页(第1天:使用模型).当我进入

ghci Model.hs
Run Code Online (Sandbox Code Playgroud)

为了将一些数据输入数据库,正如书中所说,我得到了这个:

GHCi, version 7.8.3: http://www.haskell.org/ghc/  :? for help 
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.

Model.hs:3:8:              
    Could not find module ‘ClassyPrelude.Yesod’ 
    Use -v to see a list of the files searched for.    

Model.hs:4:8:                                   
    Could not find module ‘Database.Persist.Quasi’ 
    Use -v to see a list of the files searched for.
Failed, modules loaded: none.  

我被卡住了.我是Haskell的新手(但不是新手)和Yesod,所以我不知道该怎么做.

提前致谢.

haskell yesod

1
推荐指数
1
解决办法
186
查看次数