将时间戳作为默认值存储在hstore中



我试图将当前时间戳存储为hstore中的默认值。我尝试使用now(),但所有存储的是"last_collected"=>"now()"。以下是我的文件:

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

是否有正确的方法来做到这一点,或者甚至是可能的?谢谢!

使用hstore构造函数之一可能会更容易:

hstore(text[]):从数组中构造hstore,该数组可以是键值数组,也可以是二维数组。
hstore(text[], text[]):从单独的键和值数组中构造hstore

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

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')]);

最新更新