Yesod Database Migration Cycle
I am running Yesod and accessing the MySQL database.
My model:
User
ident Text
password Text Maybe
UniqueUser ident
deriving Typeable
Email
email Text
userId UserId Maybe
verkey Text Maybe
UniqueEmail email
Comment json -- Adding "json" causes ToJSON and FromJSON instances to be derived.
message Text
userId UserId Maybe
deriving Eq
deriving Show
-- By default this file is used in Model.hs (which is imported by Foundation.hs)
When I launch the Yesod app it repeats the following:
Starting devel application
Migrating: ALTER TABLE `user` ALTER COLUMN `password` DROP DEFAULT
28/Mar/2017:23:46:44 +0200 [Debug#SQL] ALTER TABLE `user` ALTER COLUMN `password` DROP DEFAULT; []
devel.hs: ConnectionError {errFunction = "query", errNumber = 1101, errMessage = "BLOB/TEXT column 'password' can't have a default value"}
Unexpected: child process exited with ExitFailure 1
Trying again
Starting devel application
Migrating: ALTER TABLE `user` ALTER COLUMN `password` DROP DEFAULT
28/Mar/2017:23:46:46 +0200 [Debug#SQL] ALTER TABLE `user` ALTER COLUMN `password` DROP DEFAULT; []
devel.hs: ConnectionError {errFunction = "query", errNumber = 1101, errMessage = "BLOB/TEXT column 'password' can't have a default value"}
Unexpected: child process exited with ExitFailure 1
Trying again
It goes in an endless loop.
The workaround is commenting out the following line from Application.hs
, which means disabling database migration:
runLoggingT (runSqlPool (runMigration migrateAll) pool) logFunc
But if I use the workaround, I get another error:
28/Mar/2017:23:56:28 +0200 [Error#yesod-core] Foundation.hs:(137,5)-(144,45): Non-exhaustive patterns in function isAuthorized
@(yesod-core-1.4.32-6HthMZNCl0sEMRz6GJ4QO1:Yesod.Core.Class.Yesod ./Yesod/Core/Class/Yesod.hs:693:5)
28/Mar/2017:23:56:28 +0200 [Debug#SQL] SELECT `ident`,`password` FROM `user` WHERE `id`=? ; [PersistInt64 1]
GET /test
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Status: 500 Internal Server Error 0.006514s
source to share
Adding default=NULL
to each column Text Maybe
fixes this problem. In some MySQL versions, you also need to change the column type by adding sqltype=varchar(255)
. In your case, the model should look like this:
User
ident Text
password Text Maybe sqltype=varchar(255) default=NULL
UniqueUser ident
deriving Typeable
Email
email Text
userId UserId Maybe
verkey Text Maybe sqltype=varchar(255) default=NULL
UniqueEmail email
Comment json -- Adding "json" causes ToJSON and FromJSON instances to be derived.
message Text
userId UserId Maybe
deriving Eq
deriving Show
-- By default this file is used in Model.hs (which is imported by Foundation.hs)
source to share
This error seems to be related to migrations and defaults.
You can try this workaround to change MySQL behavior. In the defaults, or maybe try changing the column type from text
to varchar
?
source to share