我想知道是否有任何方法可以在rails中的UUID上创建自定义前缀。我想要产生这样的东西:
"ret_a44691c8-93e2-11e8-ac1c-1b3aa40a1cae"我找到了以下代码,我可以将其添加到迁移中以创建UUID:
t.uuid "uuid", default: "uuid_generate_v4()"但这会产生这样的情况:
"a44691c8-93e2-11e8-ac1c-1b3aa40a1cae"有可能得到我想要的格式吗?
发布于 2018-07-30 11:20:47
正如注释中提到的,postgres将UUID定义为(https://www.postgresql.org/docs/9.1/static/datatype-uuid.html)
A sequence of lower-case hexadecimal digits, in several groups separated by
hyphens, specifically a group of 8 digits followed by three groups of 4 digits
followed by a group of 12 digits, for a total of 32 digits representing the 128 bits因此,您不能在UUID列中前缀ret_。此外,如果您想要识别零售商和客户,则不应该为其创建另一列。
当使用连接UUID时使用SqlFiddle。会抛出一个错误。
发布于 2019-07-02 14:02:53
如果您想在UUID前加上前缀,可以在postgres中这样做:
CREATE TABLE IF NOT EXISTS my_table (
uuid TEXT NOT NULL DEFAULT concat('ret-', uuid_generate_v4())
);这将生成一个uuid,如:
'ret-a44691c8-93e2-11e8-ac1c-1b3aa40a1cae'https://stackoverflow.com/questions/51592250
复制相似问题