typeorm:在 postgresql 中自动生成的 UUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47651513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:40:20  来源:igfitidea点击:

typeorm: auto generated UUID in postgresql

postgresqltypescriptuuidtypeorm

提问by Neil Stevens

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.

我正在编写一个 REST API 并且我正在使用 typeorm 进行数据访问,我已经成功地使用了它,但是我希望在我的一个表上有一个 UUID 自动生成的主键。

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

有谁知道如何在 typeorm 中设置 UUID 类型和自动生成的列,我尝试了以下操作:

Using @PrimaryGeneratedColumn()

使用 @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 @PrimaryColumnand @Generated

使用@PrimaryColumn@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.

所以看起来我可以通过这种方式获得主列,但 typeorm 没有创建自动生成列所需的序列。

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

如果我使用,@PrimaryColumn({type: "uuid"})那么我会在表中得到一个 UUID 列,但不是和自动生成的列

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?

我看不到任何其他方法来实现这一点,所以有人可以建议这是否 a) 甚至可能 b) 如何创建自动生成的 UUID 列...请?

回答by D.Zotov

Try:

尝试:

@PrimaryGeneratedColumn("uuid")
id: string;

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

另外,如果你不需要主列,但需要生成 uuid 序列,你可以试试这个:

@Column()
@Generated("uuid")
uuid: string;

回答by Joshua Manns

As of Typeorm version 0.1.16, the decorator @PrimaryGeneratedColumnsupports uuidfor all databases.

从 Typeorm 版本开始0.1.16,装饰器@PrimaryGeneratedColumn支持uuid所有数据库。

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";.

如果您的 Postgres 版本尚未包含uuid-ossp(用于生成 UUID),您可以使用create extension "uuid-ossp";.

回答by Dan

This is what I am using for Postgres 9.6 and 10. UUID is generated by default.

这是我用于 Postgres 9.6 和 10 的。默认情况下生成 UUID。

CREATE EXTENSION pgcrypto;

CREATE TABLE my_table
(
  uuid UUID NOT NULL UNIQUE DEFAULT gen_random_uuid()
);

However, I haven't been using the UUID as my primary key. This is here mainly for migration purposes. What I do know is that its unique, and haven't had a collision yet in the development environment. You can try replacing NOT NULL and UNIQUE with PRIMARY KEY instead.

但是,我没有使用 UUID 作为我的主键。这在这里主要用于迁移目的。我所知道的是它的独特性,并且在开发环境中还没有发生冲突。您可以尝试用 PRIMARY KEY 替换 NOT NULL 和 UNIQUE。