[英]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
orBINARY LARGE OBJECT
.SQL标准定义了一种不同的二进制字符串类型,称为
BLOB
或BINARY LARGE OBJECT
。 The input format is different frombytea
, 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.