如何使用typeorm将可为空的数据库字段设置为NULL? [英] How to set a nullable database field to NULL with typeorm?

查看:1007
本文介绍了如何使用typeorm将可为空的数据库字段设置为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个很容易回答的问题,但找到答案似乎是不可能的.

This seems like such a simple question to answer, but finding an answer for this seems impossible.

我正在使用 Express 和 Typescript 为后端应用程序构建密码重置功能.我在数据库中使用 Postgres,在数据操作中使用 Typeorm.我的数据库中有一个带有这两列的 User 实体:

I am building a password reset feature for a backend application with Express and Typescript. I am using Postgres for the database and Typeorm for data manipulation. I have a User entity with these two columns in my database:

@Column({
    unique: true,
    nullable: true,
})
resetPasswordToken!: string;

@Column({ nullable: true, type: 'timestamp with time zone' })
resetPasswordExpiresAt!: Date;

当用户请求密码重置令牌时,resetPasswordTokenresetPasswordExpiresAt 字段都会填充所需的值.使用发送到用户电子邮件地址的令牌,用户可以重置他/她的密码.重置用户密码后,我想通过将它们设置为 null 来清除这两个字段:

When a user requests a password reset token the resetPasswordToken and resetPasswordExpiresAt fields get both filled with the desired values. With the token that was sent to the user's e-mail address, the user can reset his/her password. After the user's password is reset, I want to clear these two fields by setting them to null:

user.resetPasswordToken = null;
user.resetPasswordExpiresAt = null;
user.save()

但是如果我这样做 Typescript 会抱怨我分配 null 值的两行:

But if I do this Typescript complains about the two lines where I assign the null value:

类型 'null' 不能分配给类型 'string'.

Type 'null' is not assignable to type 'string'.

类型 'null' 不能分配给类型 'Date'.

Type 'null' is not assignable to type 'Date'.

如果我将实体中的列更改为接受 null 如下所示,错误就会消失:

If I change the columns in my entity to accept null like below, the errors disappear:

resetPasswordToken!: string | null;
...
resetPasswordExpiresAt!: Date | null;

但是当我启动 Express 应用程序时,Typeorm 尝试连接到我的数据库时出现以下错误:

But when I start my Express application I get the following error when Typeorm tries to connect to my database:

数据类型对象"在User.resetPasswordToken"中postgres"不支持数据库.

Data type "Object" in "User.resetPasswordToken" is not supported by "postgres" database.

如何将这些字段设置为 null?

How do I set these fields to null?

推荐答案

经过一夜好眠,我设法解决了我的问题.

After a good night rest I managed to solve my problem.

Typeorm 根据您在打字稿中为实体提供的变量的类型设置数据库字段的类型.Typeorm 将下面的代码强制转换为我的 postgres 数据库中的 varchar 字段,因为我给它一个 string 作为 typescript 中的类型.

Typeorm sets the type of the database fields based on the typing you give the variables for your entities in typescript. Typeorm casts the code below to a varchar field in my postgres database because I gave it a string as a type in typescript.

@Column({
    unique: true,
    nullable: true,
})
resetPasswordToken!: string;

这也是我的问题所在.Typeorm 接受字段的类型并尝试根据它读取的类型创建该数据库字段.虽然下面的代码是正确的,但 typescript 基本上将这两种类型都封装在一个 object 中,并且该对象是 Typeorm 正在读取的内容,导致了我得到的错误.

This is also where lies my problem. Typeorm takes the typing of a field and tries to create that database field based on the typing it reads. While the code below is correct, typescript basically encapsulates both types in a single object and that object is what is being read by Typeorm causing the error that I got.

resetPasswordToken!: string | null;

为了解决我的问题,我必须像这样明确指定数据库字段类型:

To fix my problem I had to specifiy the database field type explicitly like this:

@Column({
    type: 'text',
    unique: true,
    nullable: true,
})
resetPasswordToken!: string;

这篇关于如何使用typeorm将可为空的数据库字段设置为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆