ActiveRecord在Heroku上加载二进制字段不正确,在OSX上正常 [英] ActiveRecord loads binary field incorrectly on Heroku, fine on OSX
问题描述
我有一个rails 3.1应用程序,可以在Postgresql数据库中的二进制字段中存储图像(我知道在数据库中存储图像的潜在问题,但现在必须这样做)。一切在开发模式和OSX上的规范都可以正常工作,但所有图像在部署到Heroku的应用程序中都被破坏。我已经验证了数据库中的数据是正确的,指向我的本地机器在与heroku实例使用的相同的数据库,所有的图像正确显示。
所以,问题似乎在于从数据库加载数据的ActiveRecord(在Heroku上运行)。我也猜测这是一个编码问题。在本地运行rails控制台我可以验证这些字段的字节大小是否正确,但是在Heroku上运行rails控制台会显示不正确的字节大小。实际上,通过ActiveRecord在Heroku上加载一个N字节文件会导致所有文件的字节大小为2N + 1。
非常感谢任何帮助。
2n + 1的味道就像你从 bytea
而不是旧的转义格式。我猜想你正在使用一个专用的数据库,这意味着PostgreSQL 9.0有一个不同的对于bytea的默认编码:
当从PostgeSQL 8.x迁移到PostgreSQL 9.x时,可以遇到二进制问题字符串兼容性版本9中的默认表示形式为十六进制,但版本8设置为转义。您可以通过手动设置bytea_output使PostgreSQL 9使用转义。
如果我是对的,那么您可以使用上述链接中的说明或使用此汇总版本:
- 在命令行中键入 heroku config vars 以获取系统详细信息。
- 从您的
DATABASE_URL
中提取PostgreSQL用户名,看起来像postgres:// username:password @ host / DATABASE_NAME
。 - 使用
heroku pg:psql
获取PostgreSQL控制台。 - 执行
ALTER ROLE用户名SET bytea_output TO'escape';
从psql
控制台,当然,用户名
是(1)的用户名。 - 退出
psql
执行heroku重新启动
重新启动您的应用程序。
然后再试一次,希望你会得到正确的字节。 / p>
I have a rails 3.1 app that stores images in a binary field in a postgresql database (I am aware of potential issues with storing images in a database, but have to do so for now). Everything works fine locally in development mode and specs on OSX, but all images are broken in the app deployed to Heroku. I've verified that the data in the database is correct by pointing my local machine at the same database that the heroku instance uses, and all images displayed correctly.
So, the problem seems to lie in ActiveRecord (running on Heroku) loading the data from the database. I'm also guessing it is an encoding issue. Running the rails console locally I can verify that the bytesize of these fields are correct, but running the rails console on Heroku shows an incorrect bytesize. In fact, loading an N byte file via ActiveRecord on Heroku results in a bytesize of 2N+1 for all files.
Any help is greatly appreciated.
The 2n+1 smells like you're getting hex output from your bytea
instead of the old escaped format. I'm guessing that you're using a dedicated database and that means PostgreSQL 9.0 which has a different default encoding for bytea:
When migrating from PostgeSQL 8.x to PostgreSQL 9.x you can run into issues with binary string compatibility. The default representation is hex in version 9 but version 8 it is set to escaped. You can make PostgreSQL 9 use escaped by manually setting bytea_output.
If I'm right then you can use the instructions in the above link or use this summarized version:
- In the command line type "heroku config vars" to get your system details.
- Extract the PostgreSQL username from your
DATABASE_URL
which looks likepostgres://username:password@host/database_name
. - Use
heroku pg:psql
to get a PostgreSQL console. - Execute
ALTER ROLE username SET bytea_output TO 'escape';
from the thepsql
console where, of course,username
is the username from (1). - Exit
psql
do aheroku restart
to restart your application.
Then try again and hopefully you'll get the right bytes.
这篇关于ActiveRecord在Heroku上加载二进制字段不正确,在OSX上正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!