簡體   English   中英

Haskell Esqueleto 3表聯接

[英]Haskell Esqueleto 3 table join

這些是我嘗試從三個表進行SELECT的嘗試。 但是它們不編譯,而且我不理解該錯誤(我不知道為什么它期望一個元組(Entity Issue, b0)而不是我認為代碼嘗試獲取的三元組)。

嘗試1:

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}

module Handler.Support where

import           Import hiding ((==.))
import qualified Database.Esqueleto as E
import           Database.Esqueleto      ((^.), (==.), (&&.))
import           Data.Traversable

getSupportR :: CustomerId -> Handler Html
getSupportR customerId = do
  customer_issues_followUps_list <- runDB $
    E.select $
    E.from $ \(i, f, c) -> do
    E.where_ (i ^. IssueCustomerId ==. E.val customerId &&. i ^. IssueId ==. f ^. FollowUpIssueId &&. i ^. IssueCustomerId ==. c ^. CustomerId)
    return (i, f, c)
  let issues = map listToMaybe . group . sort . fst . unzip $ customer_issues_followUps_list
  defaultLayout $ do
    setTitle "Your Licenses"
    $(widgetFile "support-display")

錯誤1:

/home/hhefesto/dev/laurus-nobilis/src/Handler/Support.hs:41:5: error:
    • Couldn't match type ‘(ra, rb, rc)’ with ‘(Entity Issue, b0)’
        arising from a functional dependency between:
          constraint ‘Database.Esqueleto.Internal.Sql.SqlSelect
                        (E.SqlExpr (Entity Issue), E.SqlExpr (Entity FollowUp),
                         E.SqlExpr (Entity Customer))
                        (Entity Issue, b0)’
            arising from a use of ‘E.select’
          instance ‘Database.Esqueleto.Internal.Sql.SqlSelect
                      (a3, b3, c) (ra3, rb3, rc3)’
            at <no location info>
    • In the second argument of ‘($)’, namely
        ‘E.select
           $ E.from
               $ \ (i, f, c)
                   -> do E.where_
                           (i ^. IssueCustomerId ==. E.val customerId
                              &&.
                                i ^. IssueId ==. f ^. FollowUpIssueId
                                  &&. i ^. IssueCustomerId ==. c ^. CustomerId)
                         return (i, f, c)’
      In a stmt of a 'do' block:
        customer_issues_followUps_list <- runDB
                                            $ E.select
                                                $ E.from
                                                    $ \ (i, f, c)
                                                        -> do E.where_
                                                                (i ^. IssueCustomerId
                                                                   ==. E.val customerId
                                                                   &&.
                                                                     i ^. IssueId
                                                                       ==. f ^. FollowUpIssueId
                                                                       &&.
                                                                         i ^. IssueCustomerId
                                                                           ==. c ^. CustomerId)
                                                              return (i, f, c)
      In the expression:
        do customer_issues_followUps_list <- runDB
                                               $ E.select $ E.from $ \ (i, f, c) -> do ...
           let issues
                 = map listToMaybe . group . sort . fst . unzip
                     $ customer_issues_followUps_list
           defaultLayout
             $ do setTitle "Your Licenses"
                  (do ...)
   |
41 |     E.select $
   |     ^^^^^^^^^^...

嘗試2:

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}

module Handler.Support where

import           Import hiding ((==.))
import qualified Database.Esqueleto as E
import           Database.Esqueleto      ((^.), (==.), (&&.))
import           Data.Traversable

getSupportR :: CustomerId -> Handler Html
getSupportR customerId = do
  customer_issues_followUps_list <- runDB $
    E.select $
    E.from $ \(i `E.InnerJoin` f `E.InnerJoin` c) -> do
    E.on (c ^. CustomerId ==. i ^. IssueCustomerId)
    E.on (i ^. IssueId ==. f ^. FollowUpIssueId)
    E.where_ (i ^. IssueCustomerId ==. E.val customerId)
    return (i, f, c)
  let issues = map listToMaybe . group . sort . fst . unzip $ customer_issues_followUps_list
  defaultLayout $ do
    setTitle "Your Licenses"
    $(widgetFile "support-display")

錯誤2:

/home/hhefesto/dev/laurus-nobilis/src/Handler/Support.hs:40:5: error:
    • Couldn't match type ‘(ra, rb, rc)’ with ‘(Entity Issue, b0)’
        arising from a functional dependency between:
          constraint ‘Database.Esqueleto.Internal.Sql.SqlSelect
                        (E.SqlExpr (Entity Issue), E.SqlExpr (Entity FollowUp),
                         E.SqlExpr (Entity Customer))
                        (Entity Issue, b0)’
            arising from a use of ‘E.select’
          instance ‘Database.Esqueleto.Internal.Sql.SqlSelect
                      (a2, b2, c) (ra2, rb2, rc2)’
            at <no location info>
    • In the second argument of ‘($)’, namely
        ‘E.select
           $ E.from
               $ \ (i `E.InnerJoin` f `E.InnerJoin` c)
                   -> do E.on (c ^. CustomerId ==. i ^. IssueCustomerId)
                         E.on (i ^. IssueId ==. f ^. FollowUpIssueId)
                         ....’
      In a stmt of a 'do' block:
        customer_issues_followUps_list <- runDB
                                            $ E.select
                                                $ E.from
                                                    $ \ (i `E.InnerJoin` f `E.InnerJoin` c)
                                                        -> do E.on
                                                                (c ^. CustomerId
                                                                   ==. i ^. IssueCustomerId)
                                                              E.on
                                                                (i ^. IssueId
                                                                   ==. f ^. FollowUpIssueId)
                                                              ....
      In the expression:
        do customer_issues_followUps_list <- runDB
                                               $ E.select
                                                   $ E.from
                                                       $ \ (i `E.InnerJoin` f `E.InnerJoin` c)
                                                           -> do ...
           let issues
                 = map listToMaybe . group . sort . fst . unzip
                     $ customer_issues_followUps_list
           defaultLayout
             $ do setTitle "Your Licenses"
                  (do ...)
   |
40 |     E.select $
   |     ^^^^^^^^^^...

這是我的持久模型:

Customer
    email Text
    password Text
    firstName Text
    lastName Text
    address1 Text
    address2 Text
    city Text
    state Text
    zipCode Text
    country Text
    phone Text
    organization Text
    UniqueCustomer email
    deriving Typeable
    deriving Show
    deriving Eq
    deriving Ord
License
    licenseAlias Text
    expirationDate UTCTime
    assignedTo CustomerId
    customerId CustomerId
    deriving Show
    deriving Eq
    deriving Ord
Issue
    customerId CustomerId
    issueSummary Text
    issueDetails Text
    issueState Int
    issueDate UTCTime
    deriving Show
    deriving Eq
    deriving Ord
FollowUp
    issueId IssueId
    followUpDate UTCTime
    followUpAuthor CustomerId
    followUpText Text
    deriving Show
    deriving Eq
    deriving Ord


-- Soon to be deleted:
Email
    email Text
    customerId CustomerId Maybe
    verkey Text Maybe
    UniqueEmail email
Comment json -- Adding "json" causes ToJSON and FromJSON instances to be derived.
    message Text
    customerId CustomerId Maybe
    deriving Eq
    deriving Show

如您所見,兩次嘗試中的錯誤是相同的:它期望使用元組而不是三元組。

任何幫助將不勝感激 :)

罪魁禍首在let陳述中:

let issues = map listToMaybe . group . sort . fst . unzip $ customer_issues_followUps_list

fstunzip與元組一起使用,因此編譯器暗示customer_issues_followUps_list是元組。

為了解決這個問題,只需加上“-extra”你package.yml依賴(或者你的陰謀文件)並更換fstunzipfst3unzip3let像這樣的語句:

let issues = map listToMaybe . group . sort . fst3 . unzip3 $ customer_issues_followUps_list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM