Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeORM: auto generated UUID in PostgreSQL

Tags:

I am writing an REST API and for data access I am using typeorm, I have used this successfully but I would like to have a UUID auto-generated primary key on one of my tables.

Does anyone know how to setup a column in typeorm that is a UUID type and auto-generated, I have tried the following:

Using @PrimaryGeneratedColumn()

@PrimaryGeneratedColumn() id: string; 

This gives me an exception when synchronising with the database

TypeORM connection error: Error: column "id" cannot be cast automatically to type integer app.ts:65 at new QueryFailedError (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/typeorm/error/QueryFailedError.js:27:28) at Query.callback (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:216:38) at Query.handleError (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/pg/lib/query.js:143:17) at Connection.connectedErrorHandler (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/pg/lib/client.js:132:26) at emitOne (events.js:115:13) at Connection.emit (events.js:210:7) at Socket.<anonymous> (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/pg/lib/connection.js:118:12) at emitOne (events.js:115:13) at Socket.emit (events.js:210:7) at addChunk (_stream_readable.js:266:12) 

Using @PrimaryColumn and @Generated

@PrimaryColumn({type:"uuid"}) @Generated("uuid") id: string; 

I get the following error when attempting this

TypeORM connection error: Error: sequence "SystemUser_id_seq" does not exist app.ts:65 at new QueryFailedError (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/typeorm/error/QueryFailedError.js:27:28) at Query.callback (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:216:38) at Query.handleError (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/pg/lib/query.js:143:17) at Connection.connectedErrorHandler (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/pg/lib/client.js:132:26) at emitOne (events.js:115:13) at Connection.emit (events.js:210:7) at Socket.<anonymous> (/Users/neilstevens/repositories/Capp.co/capp/ms/ms-token-server/node_modules/pg/lib/connection.js:118:12) at emitOne (events.js:115:13) at Socket.emit (events.js:210:7) at addChunk (_stream_readable.js:266:12) 

So it looks like this way I can get a primary column but typeorm is not creating the sequence required for this to be an auto-generated column.

If I use @PrimaryColumn({type: "uuid"}) then I do get a UUID column in the table but NOT and auto-generated column

I cannot see any other way to achieve this so could someone please advise if this is a) even possible and b) how one would go about creating a auto-generated UUID column...please?

like image 309
Neil Stevens Avatar asked Dec 05 '17 10:12

Neil Stevens


People also ask

Can Postgres auto generate UUID?

Unfortunately, while PostgreSQL is great for storing and comparing UUID data, it lacks capabilities for creating UUID values in its core. Instead, it relies on third-party modules to create UUIDs using specified techniques.

Is UUID automatically generated?

Any time a row is inserted into this table, the id value will be an auto-generated UUID.

Does Postgres have UUID?

UUID is an abbreviation for Universal Unique Identifier defined by RFC 4122 and has a size of 128-bit. It is created using internal algorithms that always generate a unique value. PostgreSQL has its own UUID data type and provides modules to generate them.

What is PostgreSQL UUID?

What is the Postgres UUID Type? The Postgres UUID data type stores UUIDs as defined by RFC 4122, which are 128-bit quantities generated by algorithms that minimize the probability of having duplicate identifiers. A UUID comprises of 32 hexadecimal digits, represented in groups of 8,4,4,4 and 12.


2 Answers

Try:

@PrimaryGeneratedColumn("uuid") id: string; 

Also, if you don't need a primary column, but need to generate uuid sequence, you can try this:

@Column() @Generated("uuid") uuid: string; 
like image 137
D.Zotov Avatar answered Sep 21 '22 04:09

D.Zotov


As of Typeorm version 0.1.16, the decorator @PrimaryGeneratedColumn supports uuid for all databases.

Usage:

@Entity() class MyClass {    @PrimaryGeneratedColumn('uuid')   id: string;  } 

If your version of Postgres doesn't already include uuid-ossp (used to generate the UUID), you can install it using create extension "uuid-ossp";.

like image 42
Joshua Manns Avatar answered Sep 19 '22 04:09

Joshua Manns