Rails 3,Heroku:Taps Server错误:PGError:错误:用于编码“ UTF8”的无效字节序列:0xba [英] Rails 3, Heroku: Taps Server Error: PGError: ERROR: invalid byte sequence for encoding "UTF8": 0xba

查看:91
本文介绍了Rails 3,Heroku:Taps Server错误:PGError:错误:用于编码“ UTF8”的无效字节序列:0xba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3.0.9应用程序,它既可以在我的开发环境中本地运行,又可以在heroku应用程序中远程运行。我有将CSV文件导入模型的方法,并且该文件可以包含非英语字符,例如°,á,é,í等(西班牙语)。

I have a Rails 3.0.9 application running both locally in my dev env and remotely on a heroku app. I have a method that imports a CSV file into a model, and this file can contain non-english characters, like °,á,é,í, etc (it's in spanish).

我目前能够导入完整的文件(75k条记录),而在我的本地dev(SQLite)数据库中没有任何问题;但是,当使用 heroku db:push 将数据库上传到heroku时,它失败,并出现我在标题中张贴的错误

I am currently able to import the complete file (75k records) without any problems in my local dev (SQLite) database; but, when uploading the db to heroku with heroku db:push, it fails with the error I'm posting in the title:

!!! Caught Server Exception

HTTP CODE: 500
Taps Server Error: PGError: ERROR:  invalid byte sequence for encoding "UTF8": 0xba
HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".

显然,Heroku在插入°字符时遇到了问题。 (此文件目前没有á,é,í等字符,但我怀疑这些字符也可能会失败。)

Apparently, Heroku has issues inserting the '°' character. (At the moment the file doesn't have any á,é,í, etc characters, but I suspect these might fail too.)

我在 application.rb 使用默认编码,如下所示:

I have set in my application.rb file the default encoding, as follows:

#.../application.rb
config.encoding = "utf-8"

还有什么可以我确实要设置客户端编码并解决此问题?

What else can I do to set the 'client encoding' and solve this problem?

推荐答案

数字符号º ISO-8869-1 而不是UTF-8。因此,您的CSV文件使用Latin-1进行了编码,但是您尝试将其以UTF-8格式存储在数据库中,而没有固定编码。

The numero sign, º, is 0xBA in ISO-8869-1 not UTF-8. So your CSV file is encoded with Latin-1 but you're trying to store it in your database as UTF-8 without fixing the encoding.

您可以尝试告诉CSV库它正在处理Latin-1编码的文本,也许它会处理转换为UTF-8的问题。如果那不起作用,则可以使用 Iconv

You can try telling your CSV library that it is dealing with Latin-1 encoded text and maybe it will take care of converting to UTF-8. If that doesn't work, then you can do it yourself with Iconv:

ruby-1.9.2 > Iconv.iconv('UTF-8', 'ISO-8859-1', "\xba")
 => ["º"]
ruby-1.9.2 > Iconv.iconv('UTF-8', 'ISO-8859-1', "\xb0")
 => ["°"]

您在使用SQLite时不会遇到麻烦,因为SQLite往往非常宽容并且有一个非常宽松的类型系统。 PostgreSQL,OTOH,往往比较严格,如果尝试向其提供无效数据,则会抱怨。如果要部署到Heroku和PostgreSQL,我建议您停止在SQLite上进行开发,因为其他差异也会引起问题(例如GROUP BY和LIKE的行为)。

You're not having trouble with SQLite because SQLite tends be very forgiving and it has a very loose type system. PostgreSQL, OTOH, tends to be rather strict and properly complains if you try to feed it invalid data. I'd recommend that you stop developing on top of SQLite if you're going to be deploying to Heroku and PostgreSQL, there are other differences that will cause problems (the behavior of GROUP BY and LIKE for example).

这篇关于Rails 3,Heroku:Taps Server错误:PGError:错误:用于编码“ UTF8”的无效字节序列:0xba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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