Skip to content

Commit

Permalink
Fixed sql error when inserting an array of uuids. Fixes #593
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Dec 9, 2020
1 parent bee7b44 commit de50316
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion IHP/SchemaCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import qualified Text.Countable as Countable
import qualified IHP.IDE.SchemaDesigner.Parser as SchemaDesigner
import IHP.IDE.SchemaDesigner.Types
import Control.Monad.Fail
import qualified IHP.IDE.SchemaDesigner.Compiler as SqlCompiler

compile :: IO ()
compile = do
Expand Down Expand Up @@ -352,7 +353,17 @@ compileCreate table@(CreateTable { name, columns }) =
let
modelName = tableNameToModelName name
columnNames = commaSep (map (get #name) columns)
values = commaSep (map (const "?") columns)
values = commaSep (map columnPlaceholder columns)

-- When we do an INSERT query like @INSERT INTO values (uuids) VALUES (?)@ where the type of @uuids@ is @UUID[]@
-- we need to add a typecast to the placeholder @?@, otherwise this will throw an sql error
-- See /~https://github.com/digitallyinduced/ihp/issues/593
columnPlaceholder column@(Column { columnType }) = if columnPlaceholderNeedsTypecast column
then "? :: " <> SqlCompiler.compilePostgresType columnType
else "?"
where
columnPlaceholderNeedsTypecast Column { columnType = PArray {} } = True
columnPlaceholderNeedsTypecast _ = False

toBinding column@(Column { name }) =
if hasExplicitOrImplicitDefault column
Expand Down

0 comments on commit de50316

Please sign in to comment.