gpt4 book ai didi

ruby-on-rails - 你如何在 rails postgresql 数据库中创建一个 BLOB 列

转载 作者:行者123 更新时间:2023-11-29 11:23:19 25 4
gpt4 key购买 nike

我正在尝试将二进制数据存储在数据库中。 (heroku 上的 postgresql)

我知道有两种不同的方法可以在 postgresql 中存储二进制数据。一个 blob 和一个 bytea..

当我在迁移中创建表时,

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

它在数据库中创建一个 bytea 类型的列。

我的问题是..如何创建 blob 类型的记录?

我为什么要问?似乎当我向 heroku 发送一个 10 字节的文件时,它将它存储为一串十六进制值,前缀为“e”..所以我的 10 字节变成了 21。我的 10 兆文件将变成 20 兆(和一个字节), 分机, 分机, 分机...
现在这让我很困扰,但因为我并不真正关心性能。 (我已经被 PM 打败了),这不是最让我烦恼的。
真正困扰我的是;当我读出数据库的内容时,我得到了 21 个字节,而不是 10 个字节。那是无法使用的。

所以我的问题又来了。我如何在 rails/postgresql/heroku 环境中创建一个 BLOB 列?

最佳答案

bytea 是 PostgreSQL 的 BLOB 版本。来自fine manual :

The SQL standard defines a different binary string type, called BLOB or BINARY LARGE OBJECT. The input format is different from bytea, but the provided functions and operators are mostly the same.

所以 bytea 就是你想要的。就格式而言:

The bytea type supports two external formats for input and output: PostgreSQL's historical "escape" format, and "hex" format. Both of these are always accepted on input. The output format depends on the configuration parameter 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.)

因此,您看到的只是用于将数据传入数据库和从数据库中传出的文本版本。

这可能也很有趣:

关于ruby-on-rails - 你如何在 rails postgresql 数据库中创建一个 BLOB 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11445186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com