Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update persistent.asciidoc #215

Merged
merged 3 commits into from Aug 7, 2018
Merged

Update persistent.asciidoc #215

merged 3 commits into from Aug 7, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jul 19, 2018

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
Copy link
Member

psibi commented Jul 19, 2018

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
Copy link
Author

ghost commented Jul 19, 2018

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
Copy link
Member

psibi commented Jul 31, 2018

@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
Copy link
Author

ghost commented Jul 31, 2018

Oke I will do that after my holydays.

@snoyberg snoyberg merged commit d2c28ed into yesodweb:master Aug 7, 2018
@snoyberg
Copy link
Member

snoyberg commented Aug 7, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants