lykahb / groundhog

This library maps datatypes to a relational model, in a way similar to what ORM libraries do in OOP. See the tutorial https://www.schoolofhaskell.com/user/lykahb/groundhog for introduction
http://hackage.haskell.org/package/groundhog
176 stars 39 forks source link

Exporting TH declarations into another module #67

Closed ironchicken closed 7 years ago

ironchicken commented 7 years ago

I'm hoping to be able to define my types in one module and write queries using those types in another module. E.g.

{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneDeriving #-}

module Models where

import Database.Groundhog.TH
import Database.Groundhog.MySQL

data Record = Record
  { field :: String }
deriving instance Show Record

mkPersist defaultCodegenConfig [groundhog|
- entity: Record
|]

And:

module Main where

import Database.Groundhog.MySQL
import Models

main :: IO ()
main = do
  xs <- withMySQLConn defaultConnectInfo $ runDbConn $ select $ (Field ==. "foo")
  putStrLn $ head xs

But what actually happens here is:

Main.hs:8:66: error:
    • Data constructor not in scope: Field
    • Perhaps you meant variable ‘field’ (imported from Models)

Any ideas what's going on here?

lykahb commented 7 years ago

By default the generated constructor name is the capitalized record field with the suffix "Field". So, in your case, the line should be FieldField ==. "foo"