diff --git a/src/PgQuery.hs b/src/PgQuery.hs index 82fb628dc..77b8e6b63 100644 --- a/src/PgQuery.hs +++ b/src/PgQuery.hs @@ -165,9 +165,11 @@ placeholders :: String -> SqlRow -> String placeholders symbol = intercalate ", " . map (const symbol) . getRow insertClause :: Schema -> Text -> SqlRow -> QuotedSql +insertClause schema table (SqlRow []) = + ("insert into %I.%I default values returning *", [toSql schema, toSql table]) insertClause schema table row = - ("insert into %I.%I (" ++ placeholders "%I" row ++ ")", - map toSql $ cs schema : table : sqlRowColumns row) + ("insert into %I.%I (" ++ placeholders "%I" row ++ ")", + map toSql $ cs schema : table : sqlRowColumns row) <> (" values (" ++ placeholders "?" row ++ ") returning *", sqlRowValues row) diff --git a/test/Unit/PgQuerySpec.hs b/test/Unit/PgQuerySpec.hs index 8bc853e58..9efa6bfae 100644 --- a/test/Unit/PgQuerySpec.hs +++ b/test/Unit/PgQuerySpec.hs @@ -53,6 +53,12 @@ spec = around dbWithSchema $ do ("nullable_string", toSql ("a string"::String))]) conn `shouldThrow` \e -> seState e == "23502" + it "generates a default values query if no data is provided" $ \c -> do + r <- insert "1" "items" (SqlRow []) c + let [row] = toList r + quickALQuery c "select * from \"1\".items where id = ?" [snd row] + `shouldReturn` [[row]] + let {user = "jdoe"; pass = "secret"; role = "test_default_role"} describe "addUser" $ do it "adds a correct user to the right table" $ \conn -> do