【问题标题】:TypeORM: How to partialy update given entity with query runner?TypeORM:如何使用查询运行器部分更新给定实体?
【发布时间】:2020-12-22 06:51:03
【问题描述】:

如果我想部分更新我的用户名,我可以很容易地做到这一点:

await this.repository.save({ id: 1, name: 'john' });

但是当我使用 queryrunner 时,它要求所有字段都存在并抛出 missing following properties from type 'User'

await queryRunner.manager.save({ id: 1, name: 'john' }); // Error: Missing property

【问题讨论】:

    标签: typescript nestjs typeorm


    【解决方案1】:

    我猜save操作会被转换成insert,也许你需要检查sql执行。你也可以试试这个:

    await queryRunner.manager.update(User, 1, { name: "john" });
    

    【讨论】:

    • @omidh 引用自github.com/typeorm/typeorm/issues/3772,我不敢相信missing following properties from type 'User' 发生在update 操作中。你能显示sql 转换吗?而且我认为使用save 对于update 来说是个坏主意,因为typeorm 总是检查对应的记录是否存在,然后执行updateinsert,这是有成本的。
    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 2018-11-22
    • 2018-09-10
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2022-07-23
    相关资源
    最近更新 更多