I am writing simple server with servant and mysql-haskell package where I invoke some insert query, this part of code:
...
result <- try $ M.query conn ("insert into `user` (`email`,`name`,`date_joined`) values (?, ?, NOW());") [M.One $ M.MySQLText "1@mail.ru", M.One $ M.MySQLText "username"]
case result of
Right (defs,is) -> do
putStrLn "No exception occurred"
print =<< Streams.toList is
Left e -> putStrLn ("Caught exception: " ++ show (e :: M.ERRException))
...
Problem is that if I send request to insert new user, i see that it inserted in db, but there is no Right brunch invorked - i don't see "No exception occurred" in console and my client don't recieve response (just hanging) but as I said server provide real insertion to db (I see new row created) so it looks like it stucked in M.query or try function without invoking Rigth brunch..
Interesting that on second query with duplicate request it invoke the "Left" branch and I see
"Caught exception: ERRException (ERR {errCode = 1062, errState = "23000", errMsg = "Duplicate entry '1@mail.ru' for key 'uniq_site_email'"})
"
and client got the response without hanging.
I have:
GHCi, version 9.4.8
mysql Ver 8.0.36 for Linux on x86_64
Why this happens and how to fix?
Found problem: need to use M.execute vs M.query
result <- try $ M.execute conn ("insert into `user` (`email`,`name`,`date_joined`) values (?, ?, NOW());") [M.One $ M.MySQLText "1@mail.ru", M.One $ M.MySQLText "username"]
It would be nice to add some error in lib for this case.