1

我的 PostgreSQL 数据库中表的数据类型中的一个字段是一种UUID名为ItemId.

import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
import Data.DateTime (DateTime)
import Data.UUID
import GHC.Generics
import qualified Opaleye as O
import Data.Text (pack, Text)

newtype ItemId = ItemId UUID
  deriving (Show, Eq, Generic)

toItemId :: UUID -> ItemId
toItemId = ItemId

fromItemId :: ItemId -> UUID
fromItemId (ItemId x) = x

data Item' id name desc num most
  = Item {
    _itemId          :: id,
    _itemName        :: name,
    _itemDesc        :: desc,
    _numTimesOrdered :: num,
    _mostRecentOrder :: most
 }

type ItemRead = Item' ItemId Text Text Int DateTime
type ItemWrite = Item' (Maybe ItemId) Text Text (Maybe Int) (Maybe DateTime)
type ItemColRead = Item' (O.Column O.PGUuid)
                         (O.Column O.PGText)
                         (O.Column O.PGText)
                         (O.Column O.PGInt4)
                         (O.Column O.PGTimestamptz)
type ItemColWrite = Item' (Maybe (O.Column O.PGUuid))
                          (O.Column O.PGText)
                          (O.Column O.PGText)
                          (Maybe (O.Column O.PGInt4))
                          (Maybe (O.Column O.PGTimestamptz))

$(makeAdaptorAndInstance "pItem" ''Item')

itemTable :: O.Table ItemColWrite ItemColRead
itemTable = O.Table "items" (pItem Item { _itemId          = O.optional "id"
                                        , _itemName        = O.required "name"
                                        , _itemDesc        = O.required "desc"
                                        , _numTimesOrdered = O.optional "numTimesOrdered"
                                        , _mostRecentOrder = O.optional "mostRecentOrder"
                                        })

itemToPG :: ItemWrite -> ItemColWrite
itemToPG = pItem Item { _itemId          = const Nothing
                      , _itemName        = O.pgStrictText
                      , _itemDesc        = O.pgStrictText
                      , _numTimesOrdered = const Nothing
                      , _mostRecentOrder = const Nothing
                      }

但是,当我编译我的项目时,GHC 会抛出:

/home/gigavinyl/Projects/ordermage/src/Api/Item.hs:34:3: error:
    • No instance for (O.QueryRunnerColumnDefault O.PGUuid ItemId)
        arising from a use of ‘O.runInsertManyReturning’
    • In the second argument of ‘(<$>)’, namely
        ‘O.runInsertManyReturning con itemTable [itemToPG item] _itemId’
      In the second argument of ‘($)’, namely
        ‘listToMaybe
         <$> O.runInsertManyReturning con itemTable [itemToPG item] _itemId’
      In the expression:
        liftIO
        $ listToMaybe
          <$> O.runInsertManyReturning con itemTable [itemToPG item] _itemId

哪里src/Api/Item.hs是:

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE OverloadedStrings #-}

module Api.Item where

import Control.Monad.IO.Class (liftIO)
import Data.Maybe (listToMaybe)
import Database.PostgreSQL.Simple (Connection)
import Models.Item
import Queries.Item
import Servant
import qualified Opaleye as O

type ItemApi =
   Get '[JSON] [ItemRead]                                      :<|>
   Capture "itemId" ItemId     :> Get '[JSON] (Maybe ItemRead) :<|>
   ReqBody '[JSON] ItemWrite   :> Post '[JSON] (Maybe ItemId)

itemServer :: Connection -> Server ItemApi
itemServer con =
  getItems con    :<|>
  getItemById con :<|>
  postItem con

getItems :: Connection -> Handler [ItemRead]
getItems con = liftIO $ O.runQuery con itemsQuery

getItemById :: Connection -> ItemId -> Handler (Maybe ItemRead)
getItemById con itemID = liftIO $ listToMaybe <$> O.runQuery con (itemByIdQuery itemID)

postItem :: Connection -> ItemWrite -> Handler (Maybe ItemId)
postItem con item = liftIO $ listToMaybe <$>
  O.runInsertManyReturning con itemTable [itemToPG item] _itemId

我对 Haskell 还是很陌生,但问题似乎是 Opaleye 不知道如何转换ItemId为 aPGUuid但我知道它可以转换UUIDPGUuid. 我将如何编写实例以允许 Opaleye 进行此转换?

4

1 回答 1

2

问题似乎是 Opaleye 不知道如何将 ItemId 转换为 PGUuid 但我知道它可以将 UUID 转换为 PGUuid

反之亦然。它试图将 a 转换Column PGUuid为 anItemId并且它只知道如何将其转换为UUID. 一种方法是自己添加实例:

instance O.QueryRunnerColumnDefault O.PGUuid ItemId where
  queryRunnerColumnDefault =
         O.queryRunnerColumn id ItemId queryRunnerColumnDefault

另一种方法是使ItemId多态:

newtype ItemId' a = ItemId a
$(makeAdaptorAndInstance "pItemId" ''ItemId')

然后您可以在 Haskell 端和 Opaleye 端都使用它,而无需编写额外的实例。

于 2017-04-13T22:40:07.090 回答