簡體   English   中英

使用 Hasql.TH 時沒有 (Data.Vector.Generic.Base.Vector...) 的實例

[英]No instance for (Data.Vector.Generic.Base.Vector ...) when using Hasql.TH

我有一個使用 Hasql.TH 在 Hasql 中運行的查詢,我遇到了一個我以前從未見過的錯誤(見下文)。 我看到這個 SO question/answer引用了vector的版本,但是(a)我正在使用stack所以這些事情應該得到解決(對嗎?); (b) vector的版本沒有在錯誤中突出顯示,我沒有在我的package.yaml中固定版本,所以我不認為這是問題所在(但我不確定如何驗證這一點)。

所以,這是否與庫的版本有關(我從未見過這種事情發生),或者我是否遺漏了另一個問題?

查詢如下:

writeEvents :: [EventRow 'NewRecord] -> Session [EventID]
writeEvents =
  flip statement $
    dimap
      (unzip7 . map eventToRow)
      (toList . fmap (mkEventID . toInteger))
      [vectorStatement|
    insert into events (
      title,
      description,
      url,
      venue_id,
      start_date,
      end_date,
      origination
    ) (
      select 
        cols.title,
        cols.description,
        cols.url,
        venues.id,
        cols.start_date,
        cols.end_date,
        cols.origination
      from unnest (
        $1 :: text[],
        $2 :: text?[],
        $3 :: text?[],
        $4 :: text[],
        $5 :: timestamptz[],
        $6 :: timestamptz[],
        $7 :: text[]
      ) as cols (title, description, url, venue_name, start_date, end_date, origination)
      left join venues on (cols.venue_name = venues.name)
    )
    returning (id :: int4)
  |]
1. No instance for (Data.Vector.Generic.Base.Vector [] (Maybe Text))
       arising from a use of ‘Data.Vector.Generic.foldl'’
   • In the first argument of ‘hasql-1.6.3:Hasql.Private.Encoders.dimension’, namely
       ‘Data.Vector.Generic.foldl'’
     In the first argument of ‘hasql-1.6.3:Hasql.Private.Encoders.array’, namely
       ‘((hasql-1.6.3:Hasql.Private.Encoders.dimension
            Data.Vector.Generic.foldl')
           (hasql-1.6.3:Hasql.Private.Encoders.element
              (hasql-1.6.3:Hasql.Private.Encoders.nullable
                 hasql-1.6.3:Hasql.Private.Encoders.text)))’
     In the first argument of ‘hasql-1.6.3:Hasql.Private.Encoders.nonNullable’, namely
       ‘(hasql-1.6.3:Hasql.Private.Encoders.array
           ((hasql-1.6.3:Hasql.Private.Encoders.dimension
               Data.Vector.Generic.foldl')
              (hasql-1.6.3:Hasql.Private.Encoders.element
                 (hasql-1.6.3:Hasql.Private.Encoders.nullable
                    hasql-1.6.3:Hasql.Private.Encoders.text))))’```

根據錯誤,這是因為[]沒有Vector實例(即列表仿函數)。 我需要將我的列表元組 map 轉換為向量元組。 有點煩人的是,在我的示例中,我不得不使用 7 元組,為此vector中沒有現成的 unzip7 ,因此我不得不編寫vecunzip7

vecunzip7 :: [(a, b, c, d, e, f, g)] -> (Vector a, Vector b, Vector c, Vector d, Vector e, Vector f, Vector g)
vecunzip7 xs =
  ( fromList . map (\(a, _, _, _, _, _, _) -> a) $ xs,
    fromList . map (\(_, b, _, _, _, _, _) -> b) $ xs,
    fromList . map (\(_, _, c, _, _, _, _) -> c) $ xs,
    fromList . map (\(_, _, _, d, _, _, _) -> d) $ xs,
    fromList . map (\(_, _, _, _, e, _, _) -> e) $ xs,
    fromList . map (\(_, _, _, _, _, f, _) -> f) $ xs,
    fromList . map (\(_, _, _, _, _, _, g) -> g) $ xs
  )

暫無
暫無

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

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