postgresql - 将时间戳存储为 hstore 中的默认值

标签 postgresql hstore

我正在尝试将当前时间戳存储为 hstore 中的默认值。我尝试使用 now(),但所有存储的都是 "last_collected"=>"now()"。这是我所拥有的:

'"level"=>"1", "last_collected"=>now()'::hstore

是否有正确的方法或什至可能?谢谢!

最佳答案

使用 hstore constructor functions 之一可能会更容易:

hstore(text[]): construct an hstore from an array, which may be either a key/value array, or a two-dimensional array.
hstore(text[], text[]): construct an hstore from separate key and value arrays.

所以你可以使用其中之一:

hstore(array['level', '1', 'last_collected', now()::text])
hstore(array[array['level', '1'], array['last_collected', now()::text]])
hstore(array['level', 'last_collected'], array['1', now()::text])

请记住,hstore 使用 text 作为键和值,因此您的时间戳将是一个字符串。您可能想使用 to_char而不是 ::text 将您的时间戳转换为字符串,这将使您更好地控制精度和格式。

hstore(ARRAY['level', '1', 'last_collected', to_char(CURRENT_TIMESTAMP, 'HH12:MI:SS')]);

关于postgresql - 将时间戳存储为 hstore 中的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22565707/

相关文章:

ruby - Openshift 和 net-ssh 不兼容? (2.9.3-beta1 对比 2.9.2)

ruby-on-rails - Hstore 上的 Rails Postgres 查询以避免 SQL 注入(inject)

performance - Postgres HStore 对性能的影响是什么?

django - 如何在 django (1.8) 迁移中删除索引 varchar_pattern_ops?

multithreading - 如何避免更新死锁

postgresql - Sequelize 和 2 个连接表,麻烦

postgresql - SQLAlchemy 复合类型

ruby-on-rails - 了解 "attribute_will_change!"方法

ruby-on-rails - 返回从 hstore 中提取的特定值(Postgres + hstore_translate gem)

ruby-on-rails - 如何在 Ruby on Rails 中将 find_by_sql hstore 字符串转换为哈希值