什么是“不是PersistText值”意思? [英] what does "Not a PersistText value" mean?

查看:160
本文介绍了什么是“不是PersistText值”意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的yesod,并且正在尝试通过此屏幕录像制作相同的博客项目: https://www.youtube.com/watch?v=SadfV-qbVg8 ,唯一的区别是我使用的是MariaDB而不是PostgreSQL。每当我添加一个新的博客文章并重定向到显示它的页面时,我看到这个错误:

  [Error#yesod-核心] get BlogPostKey {unBlogPostKey = SqlBackendKey {unSqlBackendKey = 5}}:字段文章:不是PersistText的值@(yesod-core-1.4.12:Yesod.Core.Class.Yesod ./Yesod/Core/Class/Yesod.hs :577:5)

究竟是什么意思?如果我查看数据库,它会正确存储所有帖子。为什么它无法从数据库加载帖子?



以下是代码



模型

 用户
ident文本
密码文本可能
唯一用户标识
派生类型
电子邮件
电子邮件文本
用户UserId也许
verkey文本也许
UniqueEmail电子邮件

BlogPost
标题文本
文章Markdown

PostDetails.hs(从DB获取帖子并显示它)

 模块Handler.PostDetails其中

导入导入

getPostDetailsR :: BlogPostId - > Handler Html
getPostDetailsR blogPostId = do
blogPost< - runDB $ get404 blogPostId
defaultLayout $ do
$(widgetFilepostDetails / post)

PostNew.hs(创建一个新帖子并将其存储在数据库中,插入之后,它会使用新帖子重定向到PostDetails.hs)

 模块Handler.PostNew其中

导入导入
导入Yesod.Form.Bootstrap3
import Yesod.Text.Markdown

blogPostForm :: AForm处理程序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
$(widgetFileposts / new)

postPostNewR :: Handler Html
postPostNewR = do
((res,widget ),enctype)< - runFormPost $ renderBootstrap3 BootstrapBasicForm blogPostForm
案例res
FormSuccess blogPost - > do
blogPostId< - runDB $ insert blogPost
redirect $ PostDetailsR blogPostId
_ - > defaultLayout $(widgetFileposts / new)

我不明白为什么编译器不会赶上这个错误。当我创建一个帖子,而不是标题,我选择内部服务器错误

解决方案

原来,这是由一个在 persistent-mysql 包中的bug现在已经被固定在 persistent-mysql-2.3 中。



以下是感兴趣的人的根本原​​因:

MySQL C库(以及扩展名为Haskell mysql package,其中 persistent-mysql 依赖于)不区分类型级别的二进制文本和文本数据。因此,如果您将 TEXT 值保存到数据库,那么当它通过持久性查找时,它似乎是二进制数据(a PersistByteString )。



这已在#451 ,通过检查该列的字符集,MySQL API文档建议将其作为适当的解决方案。



有关更多详细信息,请参阅请求或此问题



感谢您提出这个问题;否则我不会意识到有一个错误。


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?

Here's the code

model

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 (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 (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"

解决方案

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:

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).

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值”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆