user6216224
user6216224

Reputation:

Should I generate UUID's in application or in the database?

PostgreSQL will be used in production and it has native support for UUID's.

But how should I handle generation of them?

Currently I use following:

uuid = Column(UUID, primary_key=True, default=lambda: uuid4().hex)

With this they are generated by the application.

Would it be a performance improvement if the database generates them and how could I implement that?

Upvotes: 2

Views: 2322

Answers (1)

Martin
Martin

Reputation: 6032

The great advantage of uuids is that they can be generated on the client.

This means that, for instance, you could create an invoice, with all the invoice lines, without having to hit the database to get an invoice id first.

As such, you should not generate it in the database, unless you are doing so, using a stored procedure that will insert data in more than a table.

Upvotes: 5

Related Questions