ActiveRecord在Heroku上加载二进制字段不正确,在OSX上正常 [英] ActiveRecord loads binary field incorrectly on Heroku, fine on OSX

查看:168
本文介绍了ActiveRecord在Heroku上加载二进制字段不正确,在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使用转义。


如果我是对的,那么您可以使用上述链接中的说明或使用此汇总版本:


  1. 在命令行中键入 heroku config vars 以获取系统详细信息。

  2. 从您的 DATABASE_URL 中提取PostgreSQL用户名,看起来像 postgres:// username:password @ host / DATABASE_NAME

  3. 使用 heroku pg:psql 获取PostgreSQL控制台。

  4. 执行 ALTER ROLE用户名SET bytea_output TO'escape'; psql 控制台,当然,用户名是(1)的用户名。

  5. 退出 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:

  1. In the command line type "heroku config vars" to get your system details.
  2. Extract the PostgreSQL username from your DATABASE_URL which looks like postgres://username:password@host/database_name.
  3. Use heroku pg:psql to get a PostgreSQL console.
  4. Execute ALTER ROLE username SET bytea_output TO 'escape'; from the the psql console where, of course, username is the username from (1).
  5. Exit psql do a heroku restart to restart your application.

Then try again and hopefully you'll get the right bytes.

这篇关于ActiveRecord在Heroku上加载二进制字段不正确,在OSX上正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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