简体   繁体   English

如何在rails postgresql数据库中创建BLOB列

[英]How do you create a BLOB column in rails postgresql database

I'm attempting to store binary data in a database. 我正在尝试将二进制数据存储在数据库中。 (postgresql on heroku) (关于heroku的postgresql)

I understand there are two different ways to store binary data in postgresql. 据我所知,有两种不同的方法可以在postgresql中存储二进制数据。 A blob and a bytea.. blob和bytea ..

When I create a table in my migration, 当我在迁移中创建表时,

create_table :binaries do |t|
  t.binary :data
end

it creates a column in the database of type bytea. 它在bytea类型的数据库中创建一个列。

My question is.. How do I create a record of type blob? 我的问题是..如何创建blob类型的记录?

Why do I ask? 我为什么这么问? It seems when I send a ten byte file up to heroku, it stores it as a string of hex values, prepended with an "e".. so my 10 bytes becomes 21. My 10 meg file would become 20 megs (and one byte), ext, ext, ext... 似乎当我发送一个十字节文件到heroku时,它将它存储为一个十六进制值的字符串,前面带有“e”..所以我的10个字节变为21.我的10兆字节文件将变为20兆(和一个字节) ),分机,分机,分机......
Now that bothers me, but as I don't really care about performance. 现在困扰我,但因为我并不真正关心表现。 (I've had the care beaten out of me by the PM), its not what bothers me the most. (我已经被PM打败了我),这不是最困扰我的。
What really bothers me is; 真正困扰我的是; when I read out the contents of the database I get the 21 bytes, not the 10. That is un-useable. 当我读出数据库的内容时,我得到21个字节,而不是10.这是不可用的。

So my question again.. How do I create a BLOB column in rails/postgresql/heroku environment? 所以我的问题再次出现..如何在rails / postgresql / heroku环境中创建BLOB列?

bytea is PostgreSQL's version of a BLOB. bytea是PostgreSQL的BLOB版本。 From the fine manual : 精细手册

The SQL standard defines a different binary string type, called BLOB or BINARY LARGE OBJECT . SQL标准定义了一种不同的二进制字符串类型,称为BLOBBINARY LARGE OBJECT The input format is different from bytea , but the provided functions and operators are mostly the same. 输入格式与bytea不同,但提供的函数和运算符大致相同。

So bytea is what you want. 所以bytea就是你想要的。 As far as the format goes: 就格式而言:

The bytea type supports two external formats for input and output: PostgreSQL's historical "escape" format, and "hex" format. bytea类型支持两种输入和输出的外部格式:PostgreSQL的历史“转义”格式和“十六进制”格式。 Both of these are always accepted on input. 输入时始终接受这两个。 The output format depends on the configuration parameter bytea_output; 输出格式取决于配置参数bytea_output; the default is hex. 默认值为十六进制。 (Note that the hex format was introduced in PostgreSQL 9.0; earlier versions and some tools don't understand it.) (注意,在PostgreSQL 9.0中引入了十六进制格式;早期版本和一些工具不理解它。)

So what you're seeing is just the text versions that are used for getting data into the database and out of the database. 所以你所看到的只是用于将数据导入数据库和数据库之外的文本版本。

This might also be of interest: 这也可能是有趣的:

Blob storage is built into Postgres and is supported using the ActiveRecord adapter, but only directly through the raw connection and the lo_* methods. Blob存储内置于Postgres中,并使用ActiveRecord适配器支持,但只能直接通过原始连接和lo_*方法。 You can use lo_write, lo_open, lo_close and lo_read to create and manipulate blobs. 您可以使用lo_write, lo_open, lo_close and lo_read来创建和操作blob。 Creating a blob returns an OID which you can reference the blob in your models. 创建blob会返回一个OID,您可以在模型中引用blob。

You can add this using a migration 您可以使用迁移添加此项

rails g migration AddFileToModel file:oid

Or directly like this 或者直接这样

add_column :users, :avatar, :oid

For a working example of this you should look at the Carrierwave PostgreSQL gem . 有关这方面的工作示例,您应该查看Carrierwave PostgreSQL gem You can either build a custom solution based on that code, or just use Carrierwave directly. 您可以根据该代码构建自定义解决方案,也可以直接使用Carrierwave。 I'm currently using it and it works well. 我目前正在使用它并且效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Rails中管理Postgresql数据库连接? - How do you manage Postgresql database connections in Rails? 如何使用Rails将PostgreSQL中的时间列更改为整数列? - How do I change a time column to an integer column in PostgreSQL with Rails? 你如何在带有 PostgreSQL 的 Rails 中查询 3 个不同的模型? - How do you query 3 different models in Rails w/ PostgreSQL? 您如何在Rails中创建一系列电子邮件? - How do you create an array of emails in rails? 如何在Ruby on Rails中从数据库结果集中的字段创建数组? - How do you create an array from the fields from a database resultset in Ruby on Rails? 如何在数据库中为块创建新行,或者在Rails中为每个模型创建新模型? - How do you create a new row for a block in a database or a new model for each in Rails? Rails3-路由:如何为数据库驱动的项目创建根路径? - Rails3 - routing: How do you create a root path for a database-driven item? 如何开始在Rails应用程序中使用Postgresql数据库? - How do I begin using Postgresql database with rails applications? 如何找到开发数据库的名称? (PostgreSQL / Rails) - How do I find the name of my development database? (PostgreSQL/Rails) rails中的postgresql数据库中的camelCase列(ActiveRecord) - camelCase column in postgresql database in rails (ActiveRecord)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM