yesodweb / yesodweb.com-content

Content for the www.yesodweb.com site
http://www.yesodweb.com/
Other
67 stars 112 forks source link

Update persistent.asciidoc #215

Closed ghost closed 6 years ago

ghost commented 6 years ago

In the first example I changed some typos. The migration part had a wrong identation and it had a type definition that was not needed.

In the second example made some changes so it solves #213

psibi commented 6 years ago

Thanks for your PR! Is there any reason why you have introduced the helper function runSqlite' function ? This seems to work fine for me:

#!/usr/bin/env stack
{- stack
     --resolver lts-12.1
     --install-ghc
     runghc
     --package persistent
     --package persistent-sqlite
     --package persistent-template
 -}

{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}

import           Control.Monad.IO.Class  (liftIO)
import           Database.Persist
import           Database.Persist.Sqlite
import           Database.Persist.TH
import           Control.Monad.IO.Unlift
import           Data.Text 
import           Control.Monad.Reader
import           Control.Monad.Logger
import           Conduit

share [mkPersist sqlSettings, mkSave "entityDefs"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
|]

main :: IO ()
main = runSqlite ":memory:" $ do
    runMigration $ migrate entityDefs $ entityDef (Nothing :: Maybe Person)
    michaelId <- insert $ Person "Michael" $ Just 26
    michael <- get michaelId
    liftIO $ print michael
ghost commented 6 years ago

oke, on the last one with the migration your code works. The code before it , do not work without the helper function.

I see then this error message as described in #213

Configuring GHCi with the following packages:
GHCi, version 8.4.3: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from C:\Users\rwobb\AppData\Local\Temp\haskell-stack-ghci\2a3bbd58\ghci-script
[1 of 1] Compiling Main             ( createDatabase.hs, interpreted )

createDatabase.hs:29:8: error:
    * Couldn't match type `BaseBackend backend0' with `SqlBackend'
        arising from a use of `runSqlite'
      The type variable `backend0' is ambiguous
    * In the expression: runSqlite ":memory:"
      In the expression:
        runSqlite ":memory:"
          $ do michaelId <- insert $ Person "Michael" $ Just 26
               michael <- get michaelId
               liftIO $ print michael
      In an equation for `main':
          main
            = runSqlite ":memory:"
                $ do michaelId <- insert $ Person "Michael" $ Just 26
                     michael <- get michaelId
                     liftIO $ print michael
   |
29 | main = runSqlite ":memory:" $ do
   |        ^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.

So should I delete the helper function in the code with the migration and let it stay in the code without the migration.

psibi commented 6 years ago

@RoelofWobben Sorry for my late reply.

So should I delete the helper function in the code with the migration and let it stay in the code without the migration.

Yes.

ghost commented 6 years ago

Oke I will do that after my holydays.

snoyberg commented 6 years ago

Thanks!