Вопрос

For development I'm using H2 database, in prod it will most likely be Postgres. Is there a way to instruct, in implementation-agnostic fashion, the database to automatically provide UUIDs for table's rows?

Это было полезно?

Решение

A user defined function could be used.

Related (I know this isn't your question): Please note if you have a lot of rows in the table (millions of rows), and if you have an index on this UUID, you should avoid randomly distributed UUIDs for performance reasons. This is for all databases, except if it the index easily fits completely in memory. Because of that, I personally would avoid UUIDs and use sequences instead if ever possible.

Другие советы

Well apparently, it's as simple as that:

CREATE TABLE items (
  uuid SERIAL,
  PRIMARY KEY (uuid)
)

I didn't find SERIAL documented on H2, here's the doc for PostgreSQL. I don't know to what extent this is db-agnostic, but it works on both H2 and Postgre so good enough at the moment.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top