7
I have noticed that some places have switched the ID (generated by autoincroment, depending on SGDB) by UUID, usually use version 4 of UUID which is based on a pseudo-random system, the question is not about collision problems, which in UUID v4 has minimal chance, but rather the issue of the motivation of the use behind it, follows some examples of uses:
Entity framework
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public Guid Id { get; set; }
...
In Entity Core I believe you don’t have it yet, or maybe you won’t have it, I can’t confirm, I haven’t used it yet, so you have to solve it in the application with protected override void OnModelCreating(ModelBuilder modelBuilder)
(or other means) and apply NEWSEQUENTIALID()
(or Guid.NewGuid()
)
Hibernate
In Hibernate I believe it is something with generators something like this:
@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
...
Laravel Eloquent
In the Eloquent framework used within the Laravel and Lumen Framework (or even stand-alone) we can configure (and is usually done this way) in the Models, with or directly in the Model of interest, overwriting the method and properties $keyType
and $incrementing
:
use Illuminate\Support\Str;
...
protected $keyType = 'string';
public $incrementing = false;
protected static function bootHasUuid()
{
static::creating(function ($model) {
if (!$model->getKey()) {
$model->{$model->getKeyName()} = (string) Str::uuid();
}
});
}
I want to point out that some cases would even set in the default column without needing the application layer, as in Postgres-13 which has native UUID generator instead of postgis-script and maybe in SQL-Server with NEWSEQUENTIALID()
(I have tested none in practice).
Although all cases of solving in the application (Asp.net-mvc, JPA, php) seem to work for simple things, but I believe they do not have good efficiency for inserting data many data at the same time (I don’t know how the core of these frameworks works works works, I just assume that it is in the layer of the application that generates UUID if you are wrong you can comment), but the question I have is not about performance, but what is the purpose of using UUID version 4 as ID instead of INT+autoincrement.
Is it supposed to make it difficult to identify data sequentially? For example I have an ID 23, so I can assume another ID of the same context being 24, and so on?
Or it would be difficult to deduce the amount of data?
Or some other reason?
The
int
with auto increment is unique to only one database and only one table, whereas UUID is universally unique, even if you doselect uuid();
on two distinct machines with distinct DBMS the values returned by the function will be different. If you adopt UUID as an identifier you would have an ease when migrating the data, because theint
is unique only in the context of your table or database so you will have great chances of conflicts when you are importing to another base, while UUID gives you more guarantees on a migration.– gato
@cat I agree with that, even Piovezan quoted a link in the comments where the author points out that possibility, but personally the very process of migrating or merging data from different sources to a new or continued structure where a JOIN occurs is not so complicated to solve, if migration will occur where will unify data the application layer itself will have to have new locations, after all it is a new context, then the idea seems good, but if it is to solve a future problem it is better to learn to solve the future problem without needing it...
– Guilherme Nascimento
There was no need for migration ;D
– gato
... I mean, it’s like I create a reasonably smart bot for simple purposes, but I add inside it a bomb because I’m afraid it will dominate humanity and I could destroy it if necessary, well if that’s possible it means that I’ve created a bot that’s too exaggerated for a task that should be average and the problem would be elsewhere and should be solved in another way :P, I know that illustrating the problem so may sound exaggerated, but this is how it seems to me the very idea of using UUID-v4 so far.
– Guilherme Nascimento
Without comparing to the auto increment the motivation is that it does not require a central authority to manage them, making each UUID unique and independent of anything else.
– gato