我正在尝试将当前时间戳存储为 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/