首页 > 解决方案 > TypeORM 属性可以为空,但收到“不能为不可为空的字段 xx 返回 null”

问题描述

鉴于数据集现在不一致,我有一个所有字段都可以为空的数据模型,但我仍然想使用数据。

这是模型的一个片段:

@ObjectType()
@Entity()
export class SomeEntity extends BaseEntity {

    @PrimaryGeneratedColumn({
        type: 'bigint',
    })
    @Field()
    id: number;

    @Column({
        nullable: true,
        type: 'text'
    })
    @Field()
    var1: string


    @Column({
        nullable: true,
        type: 'text'
    })
    @Field()
    var2: string
}

但是在执行 graphQL 查询时,我收到以下错误:

 "message": "Cannot return null for non-nullable field SomeEntity.var1",

该字段为空,因为我可以在数据库上检查它,但鉴于我已将 nullable 设置为 true,我不会期待此错误。

有人有线索吗?多谢你们。

标签: typescriptgraphqltypeormtypegraphql

解决方案


推荐阅读