UUIDField的“默认”属性是否照顾到唯一性? [英] Does UUIDField's 'default' attribute takes care of the uniqueness?

查看:68
本文介绍了UUIDField的“默认”属性是否照顾到唯一性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚跳入Django进行了一个快速项目,我发现其中有一个

I just jumped into Django for a quick project and I figured there is a UUIDField in the models.

我将其用于每个模型都必须公开对象的外部id字段。 default 参数是否可以处理唯一性,还是必须在保存时将其写入?我的意思是,我知道几乎没有价值冲突的机会,而只是想知道它是如何在内部完成的

I am using this for an external id field that every model will have to expose the object. Will the default parameter handle the uniqueness or do I have to write it in the save? I mean I know there is practically no chance of values colliding, but just to know how it is done internally

推荐答案

UUID模块每次如何保证唯一值?

RFC 4122 (UUID模块规范)指定了三种生成UUID的算法:

RFC 4122(UUID module specification) specifies three algorithms to generate UUIDs:


  1. 使用IEEE 802 MAC地址作为唯一性来源

  2. 使用伪随机数

  3. 将知名字符串与加密哈希结合使用

在所有情况下,种子值都与系统时钟时钟序列值结合在一起(以保持唯一性)以防时钟倒转)。结果,根据上述机制生成的UUID将与已经或将要分配的所有其他UUID保持唯一。

In all cases the seed value is combined with the system clock and a clock sequence value (to maintain uniqueness in case the clock was set backwards). As a result, the UUIDs generated according to the mechanisms above will be unique from all other UUIDs that have been or will be assigned.

来自RFC 4122 摘要

Taken from RFC 4122 Abstract:


UUID为128位长,可以保证空间和
时间的唯一性。

A UUID is 128 bits long, and can guarantee uniqueness across space and time.

注意:由于UUIDS的唯一性属性,Django内部没有进行检查(如@FlipperPA所述)检查是否已经存在另一个具有相同 uuid 的对象。

Note: Due to this uniqueness property of UUIDS, there is no check done by Django internally (as mentioned by @FlipperPA) to check if there already exists another object with the same uuid.

这篇关于UUIDField的“默认”属性是否照顾到唯一性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆