3

I created a new application and I am thinking where is the best place to generate a UUID.

  1. Generate a UUID in application level and persist it
  2. Generate a UUID in Database level

I have a feeling that is a better approach to generate the UUID in Database level.

What do you think and why?

Many thanks in advance

pik4
  • 385

2 Answers2

3

You may want to go with "both".

Generating the UUIDs in the application usually makes it easier to run tests as mocking DB logic can be quite fragile.

On the other side of things, if you're developing and need to quickly populate fake data, it's easiest if the UUID can be generated as part of a simple insert statement.

TL;DR: it depends on the context

Morgen
  • 1,081
0

For most applications IDs should be generated in the DB as this will prevent duplicates if you have multiple client instances, for example load balanced web servers. There are database systems which have different consistency models from SQL RDBMSs which can have the same problem but then you are making an active choice to use an inconsistent DB.