如何更新typeorm实体可为空列?

问题描述 投票:0回答:1

在 typeorm 实体中,我有此专栏:

@Column({
    type: 'bigint',
    nullable: true,
})
lockedTimestamp?: number;

在我的 Nest.js 代码中,我有这段代码:

if (...) {entity.lockedTimestamp = null;}

问题:我严格的打字稿尖叫着lockedTimestamp不能为空。 Ts 告诉我它只能是未定义的或数字。

我错过了什么?我是否正确编写了可为空的列?

如果我想用未定义的方式更新它,DB会抛出一些错误吗?

typescript nestjs typeorm
1个回答
0
投票

如果您想将其显式设置为

null
,则需要在键入时在该属性上允许
null

@Column({
    type: 'bigint',
    nullable: true,
})
lockedTimestamp?: number | null;

因为文档指出使用保存功能将跳过未定义的属性:

save - Saves a given entity or array of entities. If the entity already exist in the database, it is updated. If the entity does not exist in the database, it is inserted. It saves all given entities in a single transaction (in the case of entity, manager is not transactional). Also supports partial updating since all undefined properties are skipped. Returns the saved entity/entities.

将值设置为

null
会将其保存到
null

© www.soinside.com 2019 - 2024. All rights reserved.