【问题标题】:How to set a nullable database field to NULL with typeorm?如何使用 typeorm 将可为空的数据库字段设置为 NULL?
【发布时间】:2021-02-14 12:34:15
【问题描述】:

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

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

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

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

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

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

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

类型“null”不能分配给类型“string”。

类型“null”不能分配给类型“Date”。

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

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

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

“postgres”数据库不支持“User.resetPasswordToken”中的数据类型“Object”。

 

如何将这些字段设置为null

【问题讨论】:

标签: typescript postgresql express typeorm


【解决方案1】:
@Column('text', {
    unique: true,
    nullable: true,
})
resetPasswordToken!: string;

【讨论】:

    【解决方案2】:

    接受的答案并不完全正确。从字符串类型转换的默认 typeorm 在 MySQL DB 上是“varchar”。因此,如果您使用type: "text",它将错误地定义该列。如果你想让它与默认行为兼容,你应该使用这样的打字稿类型

    @Column({
        type: String,
        unique: true,
        nullable: true,
    })
    resetPasswordToken!: string | null;
    

    【讨论】:

      【解决方案3】:

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

      {
         name: "tag_id",
         type: "varchar",
         isNullable: true
      },
      

      【讨论】:

        【解决方案4】:

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

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

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

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

        resetPasswordToken!: string | null;
        

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

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

        【讨论】:

        • 现在可以将resetPasswordToken 输入为string | null 吗?
        • @superJustin,将type: 'text'添加到列装饰器后,您可以添加string | null进行类型检查。
        猜你喜欢
        • 1970-01-01
        • 2011-05-25
        • 2011-11-12
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 2012-05-09
        • 2019-05-08
        • 2016-02-07
        相关资源
        最近更新 更多