bitemyapp/esqueleto

Typechecked Insertion combinators generate invalid query

Open

#91 opened on May 29, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Haskell (106 forks)github user discovery
bughelp wanted

Repository metrics

Stars
 (395 stars)
PR merge metrics
 (PR metrics pending)

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]

Contributor guide