typeorm: auto generated UUID in postgresql

Neil Stevens picture Neil Stevens · Dec 5, 2017 · Viewed 23.3k times · Source

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?

Answer

D.Zotov picture D.Zotov · Dec 5, 2017

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;