问题描述
我是 yesod 的新手,我正在尝试通过此截屏视频制作相同的博客项目:https://www.youtube.com/watch?v=SadfV-qbVg8 唯一的区别是我使用的是 MariaDB 而不是 PostgreSQL.每次我添加新博客文章并重定向到显示它的页面时,我都会看到此错误:
I'm new to yesod and I'm trying to make the same blog project from this screencast: https://www.youtube.com/watch?v=SadfV-qbVg8 with the only difference being that I'm using MariaDB instead of PostgreSQL. Every time I add a new blog post and redirect to the page that shows it I see this error:
[Error#yesod-core] get BlogPostKey {unBlogPostKey = SqlBackendKey {unSqlBackendKey = 5}}: field article: Not a PersistText value @(yesod-core-1.4.12:Yesod.Core.Class.Yesod ./Yesod/Core/Class/Yesod.hs:577:5)
这到底是什么意思?如果我查看数据库,它会正确存储所有帖子.为什么无法从数据库中加载帖子?
What exactly does that mean? If I look into the database, it has all the posts stored correctly. Why does it fail to load the posts from the database?
这是代码
型号
User
ident Text
password Text Maybe
UniqueUser ident
deriving Typeable
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
BlogPost
title Text
article Markdown
PostDetails.hs(从数据库中获取帖子并显示)
PostDetails.hs (Gets the post from DB and shows it)
module Handler.PostDetails where
import Import
getPostDetailsR :: BlogPostId -> Handler Html
getPostDetailsR blogPostId = do
blogPost <- runDB $ get404 blogPostId
defaultLayout $ do
$(widgetFile "postDetails/post")
PostNew.hs(创建一个新帖子并将其存储在数据库中,插入后,它与新帖子一起重定向到 PostDetails.hs)
PostNew.hs (Creates a new post and stores it in the DB, after insertion, it redirects to PostDetails.hs with the new post)
module Handler.PostNew where
import Import
import Yesod.Form.Bootstrap3
import Yesod.Text.Markdown
blogPostForm :: AForm Handler BlogPost
blogPostForm = BlogPost
<$> areq textField (bfs ("Title" :: Text)) Nothing
<*> areq markdownField (bfs ("Article" :: Text)) Nothing
getPostNewR :: Handler Html
getPostNewR = do
(widget, enctype) <- generateFormPost $ renderBootstrap3 BootstrapBasicForm blogPostForm
defaultLayout $ do
$(widgetFile "posts/new")
postPostNewR :: Handler Html
postPostNewR = do
((res, widget), enctype) <- runFormPost $ renderBootstrap3 BootstrapBasicForm blogPostForm
case res of
FormSuccess blogPost -> do
blogPostId <- runDB $ insert blogPost
redirect $ PostDetailsR blogPostId
_ -> defaultLayout $(widgetFile "posts/new")
我不明白为什么编译器没有捕捉到这个错误.当我创建帖子时,我看到内部服务器错误"而不是标题
I don't understand why the compiler doesn't catch this error. When I create a post, instead of the title I se "Internal Server Error"
推荐答案
这原来是由 persistent-mysql
包中的错误引起的,该包现已在 persistent-mysql- 中修复2.3
.
This turned out to be caused by a bug in the persistent-mysql
package that's now fixed in persistent-mysql-2.3
.
对于那些感兴趣的人来说,这是根本原因:
Here's the root cause for those interested:
MySQL C 库(以及扩展的 Haskell mysql
包,persistent-mysql
所依赖的)不区分类型级别的二进制数据和文本数据.因此,如果您将 TEXT
值保存到数据库中,当通过持久化查找它时,它似乎是二进制数据(一个 PersistByteString
).
The MySQL C library (and by extension the Haskell mysql
package, which persistent-mysql
depends on) doesn't distinguish between binary and textual data at the type level. So if you saved a TEXT
value to the database, when it was looked it up by persistent it appeared to be binary data (a PersistByteString
).
这已在 #451 中通过检查列的字符集得到修复,其中MySQL API 文档推荐作为适当的解决方案.
This was fixed in #451 by checking the character set of the column, which the MySQL API docs recommend as the appropriate solution.
有关更多详细信息,请参阅拉取请求或此问题.
For more details, see that pull request or this issue.
感谢您提出这个问题;否则我不会意识到存在错误.
Thanks for asking this question; I wouldn't have realized there was a bug otherwise.
这篇关于“不是 PersistText 值"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!