如何在PostgreSQL中自动生成新的UUID?
我正在使用PostgreSQL版本14.4。我安装了uuid-ossp
扩展名。
我创建了这样的表:
CREATE TABLE reserved_words
ADD id uuid NOT NULL DEFAULT uuid_generate_v1()
ADD word NOT NULL varchar(20);
不幸的是,当我尝试添加新记录,而不是生成新的uuid时,而是将“ uuid_generate_v1()”字符串添加为id> id
!
我已经搜寻了互联网,但无法找到如何更改事物,以便执行功能本身。有什么想法吗?
I'm using PostgreSQL version 14.4. I installed the uuid-ossp
extension.
I created a table like this:
CREATE TABLE reserved_words
ADD id uuid NOT NULL DEFAULT uuid_generate_v1()
ADD word NOT NULL varchar(20);
Unfortunately, when I try adding a new record, rather than a new UUID being generated, instead the "uuid_generate_v1()" string is added in as the id
!
I've scoured the Internet but can't find out how to alter things so that the function itself is executed. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最重要的是,数据类型应为
uuid
'uuid-ossp'扩展名提供了以生成uuid值的功能。
要将扩展名添加到数据库中,运行以下命令
创建扩展名为“ uuid-ossp”;
可以使用核心函数gen_random_uuid()生成版本-4 uuids。
要在DBEAVER中使用此函数,请按照以下步骤操作:
1。到达您要生成UUID的表
2.在“表属性”选项卡中找到您需要应用的列
UUID函数
3.双击列名,它将显示其扩展的视图
属性
4. inder 默认该特定列属性的值,写
功能名称如图所示
gen_random_uuid()
dbeaver插图
The foremost point is that data type should be of
uuid
The 'uuid-ossp' extension offers functions to generate UUID values.
To add the extension to the database run the following command
CREATE EXTENSION "uuid-ossp";
you can use the core function gen_random_uuid() to generate version-4 UUIDs.
To make use of this function in dbeaver ,follow the steps:
1.Go to table in which you want to generate UUID's
2.In the table properties tab find the column to which you need to apply the
uuid function
3.Double click on the column name and it will show expanded view of it's
properties
4.Under default value of that particular column properties, write the
function name as shown in the image
gen_random_uuid()
Dbeaver illustration
我很抱歉,这实际上确实有效。发生的事情是,在DBEaver,DB Client I用户中,它最初显示了UUID生成功能,但是当您保存新记录时,它会正确创建UUID。
注意:我不太了解
uuid_generate_v1
和uuid_generate_v4
之间的区别,但是我将选择使用后一个。My apologies, it actually does work. What's happening is that in DBeaver, the DB client I user, it does at first show the UUID generation function but then when you save the new record, it creates the UUID correctly.
Note: I don't really understand the difference between
uuid_generate_v1
anduuid_generate_v4
but am going to opt to use the latter one.uuid_generate_v1()→uuid
生成版本1 UUID。这涉及计算机的MAC地址和时间戳。请注意,此类UUID揭示了创建标识符的计算机的身份以及它这样做的时间,这可能使其不适合某些安全敏感的应用程序。
uuid_generate_v4()→UUID
生成一个版本4 UUID,该版本完全源自随机数。
source
uuid_generate_v1 () → uuid
Generates a version 1 UUID. This involves the MAC address of the computer and a time stamp. Note that UUIDs of this kind reveal the identity of the computer that created the identifier and the time at which it did so, which might make it unsuitable for certain security-sensitive applications.
uuid_generate_v4 () → uuid
Generates a version 4 UUID, which is derived entirely from random numbers.
source