bitemyapp/esqueleto

Typechecked Insertion combinators generate invalid query

Open

#91 ouverte le 29 mai 2018

Voir sur GitHub
 (1 commentaire) (0 réactions) (0 assignés)Haskell (106 forks)github user discovery
bughelp wanted

Métriques du dépôt

Stars
 (395 stars)
Métriques de merge PR
 (Métriques PR en attente)

Description

This code compiles:

{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving         #-}
{-# LANGUAGE DeriveGeneric              #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
module Bug where

import qualified Database.Persist.MySQL as PMySQL
import Database.Persist.TH
import Database.Esqueleto
import Control.Monad.IO.Class

mkPersist sqlSettings [persistLowerCase|
  Table1 sql=table1
    col1 Int

  Table2 sql=table2
    col1 Int
    col2 Int
|]

q :: MonadIO m => SqlWriteT m ()
q = insertSelect $ from $ \t -> return (Table2 1 <# (t ^. Table1Col1))

But generates an invalid query:

INSERT INTO `table2`(`col1`, `col2`)
SELECT `table1`.`col1`
FROM `table1`
; []

*** Exception: ERRException (ERR {errCode = 1136, errState = "21S01", errMsg = "Column count doesn't match value count at row 1"})

Changing it like this works:

q = insertSelect $ from $ \t -> return (Table2 <# val 1 <&> (t ^. Table1Col1))

which generates

INSERT INTO `table2`(`col1`, `col2`)
SELECT ?, `table1`.`col1`
FROM `table1`
; [PersistInt64 1]

Guide contributeur