Storing Markdown in a Yesod Persist Model

I am trying to store markdown on a field in Persistent model with a MySQL backend. I messed up the use case:

models:

TestModel
    content Markdown

      

Model.hs:

import Text.Markdown (Markdown)
import Yesod.Text.Markdown ()
....

      

This seems to create the correct migrations:

mysql> show columns in test_model;
+---------+------------+------+-----+---------+----------------+
| Field   | Type       | Null | Key | Default | Extra          |
+---------+------------+------+-----+---------+----------------+
| id      | bigint(20) | NO   | PRI | NULL    | auto_increment |
| content | text       | NO   |     | NULL    |                |
+---------+------------+------+-----+---------+----------------+

      

However, when I try to fetch data from this table, I get an exception. Below is an example from cabal repl

, but the same happens when the application starts:

repl> testid <- db $ insert $ TestModel $ Markdown "# Hello world"
[Debug#SQL] INSERT INTO `test_model`(`content`) VALUES(?); [PersistText "# Hello world"]
repl> testmodel <- db $ get testid
[Debug#SQL] SELECT `content` FROM `test_model` WHERE `id`=?; [PersistInt64 2]
*** Exception: runFakeHandler issue: InternalError "get TestModelKey {unTestModelKey = SqlBackendKey {unSqlBackendKey = 2}}: field content: Not a PersistText value"

      

My first guess was that the TestModel constructor value was wrong, but the same error occurs even when creating an object using markdownField

in the form:

testModelForm :: AForm Handler TestModel
testModelForm = TestModel
        <$> areq markdownField "Content" Nothing

getTestModelR :: TestModelId -> Handler Html
getTestModelR testModelId = do
    testmodel <- runDB $ get404 testModelId
....
08/Aug/2015:12:44:33 -0700 [Error#yesod-core] get TestModelKey {unTestModelKey = SqlBackendKey {unSqlBackendKey = 2}}: field content: Not a PersistText value @(yesod-core-1.4.12:Yesod.Core.Class.Yesod ./Yesod/Core/Class/Yesod.hs:577:5)

      

Has anyone seen this exception before and knows how to deal with it? The only documentation I could find for this error was the source code for yesod-text-markdown

.

+3


source to share


1 answer


This turned out to be caused by a bug in the package persistent-mysql

, which is now fixed in persistent-mysql-2.3

.

Here is the main reason for those interested:

The MySQL C library (and also the extension to the Haskell package mysql

it depends on persistent-mysql

) does not distinguish between binary and text data at the type level. So if you stored the value TEXT

in the database when it looked persistent, it ended up being binary (a PersistByteString

).



I fixed this in # 451 by checking the column character set recommended by the MySQL API docs as a suitable solution.

See pull request or this issue for more information .

Thanks for asking this question; I would not have realized there was an error otherwise.

+1


source







All Articles