postgresql typeorm 中的 Postgres 枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44974594/
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
Postgres enum in typeorm
提问by Tai Tran
In typeorm, how can I create a postgres enum type Genderas in this raw query
在 typeorm 中,如何像在此原始查询中一样创建 postgres 枚举类型Gender
CREATE TYPE public.Gender AS ENUM (
'male', 'female'
);
ALTER TABLE public.person ALTER COLUMN gender TYPE public.gender USING gender::gender;
and use it in the Entity class?
并在实体类中使用它?
I have tried
我努力了
@Entity()
export class Person {
@Column('enum')
gender: 'male' | 'female'
}
but obviously this isn't the right way, since I got the error message "type enumdoes not exist".
但显然这不是正确的方法,因为我收到错误消息“类型枚举不存在”。
I don't want to use typescript enum either, since it will give me a bunch of 0s and 1s in the database.
我也不想使用 typescript 枚举,因为它会在数据库中给我一堆 0 和 1。
回答by Felipe Sabino
EDIT:This answer is still valid but a bit outdated as 0.1.0
alpha versions of TypeORM support enums for both PostgreSQLand MySQL.
编辑:这个答案仍然有效,但有点过时了,因为0.1.0
TypeORM 的 alpha 版本支持PostgreSQL和 MySQL 的枚举。
PostgreSQL
has a built in enum type, but unfortunately TypeORM
currently only supports it for MySQL.
PostgreSQL
有一个内置的枚举类型,但不幸的是TypeORM
目前只支持 MySQL。
However, you could achieve a similar result with an int-type enum by using the @Column
type as int
and using the enum for your field type.
但是,您可以通过使用@Column
类型 asint
和使用枚举作为您的字段类型,使用int 类型枚举获得类似的结果。
enum Gender {
Male,
Female,
Other
}
@Entity()
export class Person {
@Column('int')
gender: Gender
}
(This approach lets you use the @IsEnum
decorator from class-validatorto validate the input if needed)
(这种方法允许您在需要时使用@IsEnum
class-validator 中的装饰器来验证输入)
You could also use string enums (available on TypeScript 2.4, check Typescript `enum` from JSON stringfor older versions) and if that is the case just change the data type to string
instead.
您还可以使用字符串枚举(在 TypeScript 2.4 上可用,从 JSON 字符串中检查Typescript `enum`以获得旧版本),如果是这种情况,只需将数据类型更改为string
。
enum Gender {
Male = 'male',
Female = 'female',
Other = 'other'
}
@Entity()
export class Person {
@Column('text')
gender: Gender
}
回答by Javier Aviles
As the accepted answer states, it is now supported in postgres but still buggy: Github issue, the fix will be released in the next RC probably. Meanwhile, I saw on the thread a nice solution which I even liked it more than the actual feature fully working:
正如已接受的答案所述,它现在在 postgres 中受支持,但仍有问题:Github issue,该修复程序可能会在下一个 RC 中发布。同时,我在线程上看到了一个很好的解决方案,我什至更喜欢它而不是完全工作的实际功能:
fwiw I've been using string enum with check constraint. It's a lot more flexible than actual postgres enum, which creates whole new data types in postgres index and are really hard to manage (alter table, etc.)
fwiw 我一直在使用带有检查约束的字符串枚举。它比实际的 postgres 枚举灵活得多,后者在 postgres 索引中创建全新的数据类型并且很难管理(更改表等)
export function CheckEnum(tableName: string, fieldName: string, enumValue: any) {
// Hash enum value and put it as part of constraint name so we can
// force typeorm to generate migration for enum changes.
const hash = crypto
.createHash('sha1')
.update(Object.values(enumValue).join(''))
.digest('hex')
return Check(
// https://til.hashrocket.com/posts/8f87c65a0a-postgresqls-max-identifier-length-is-63-bytes
`cke_${tableName}_${fieldName}_${hash}`.slice(0, 63),
`${fieldName} in (${Object.values(enumValue).map(t => `'${t}'`)})`,
)
}
And use it like so
并像这样使用它
export enum Gender {
Male = 'male',
Female = 'female',
Other = 'other'
}
@Entity()
@CheckEnum('person', 'gender', Gender)
export class Person {
回答by William Saar
For Postgres, the column type should be 'text', not 'string', as string results in DataTypeNotSupportedError: Data type "string" in "" is not supported by "postgres" database.
对于 Postgres,列类型应该是“文本”,而不是“字符串”,因为字符串会导致 DataTypeNotSupportedError:“postgres”数据库不支持“”中的数据类型“字符串”。