作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我删除带有 softDelete
列的实体时,deletedAt
和 deletedBy
为空或未设置。
如果我尝试 softRemove
,它只会设置 deletedAt
标志。
下面是实体的一些代码:
@DeleteDateColumn({ name: 'deleted_at'})
deletedAt: Date;
@Column({ type: 'varchar', name: 'deleted_by', nullable: true })
deletedBy: string;
对于服务:
public async delete(id: string): Promise<void> {
const talent: Talent = await this.talentRepository.findOne(id);
if (!talent) throw new NotFoundException(`Talent with ID ${id} Not Found`);
talent.deletedBy = "John Doe";
await this.talentRepository.softDelete(talent);
}
如果我记录此服务,参数 deletedBy
设置为“John Doe”,但数据库为空。
最佳答案
软删除只会更新 deletedAt
列。如果您想更新 deletedBy
,您应该将其作为更新查询单独执行。
来自源代码文档:
软删除:
/**
* Records the delete date of entities by a given condition(s).
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
* Executes fast and efficient DELETE query.
* Does not check if entity exist in the database.
* Condition(s) cannot be empty.
*/
软删除:
/**
* Records the delete date of all given entities.
*/
一个可选的解决方案可能是:
public async delete(id: string): Promise<void> {
const talent: Talent = await this.talentRepository.findOne(id);
if (!talent) throw new NotFoundException(`Talent with ID ${id} Not Found`);
talent.deletedBy = "John Doe";
await this.talentRepository.save(talent);
await this.talentRepository.softDelete(talent);
}
或在交易中:
public async delete(id: string): Promise<void> {
const talent: Talent = await this.talentRepository.findOne(id);
if (!talent) throw new NotFoundException(`Talent with ID ${id} Not Found`);
talent.deletedBy = "John Doe";
await this.talentRepository
.manager
.transaction(em => {
await em.save(Talent, talent);
return em.softDelete(Talent, talent);
});
}
关于next.js - SoftDelete : Can't set flag deletedBy, deletedAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65688600/
当我删除带有 softDelete 列的实体时,deletedAt 和 deletedBy 为空或未设置。 如果我尝试 softRemove,它只会设置 deletedAt 标志。 下面是实体的一些代
我是一名优秀的程序员,十分优秀!