You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nest+TypeORM+PostgreSQL保存Chat实体报错求助

问题描述

此前向Chat中添加单个用户时可正常运行,如今即使仅添加一个用户仍出现报错,备份环境也存在相同问题。

相关代码

Chat实体

import {Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryGeneratedColumn} from "typeorm";
import {User} from "./user.entity";
import {Order} from "./order.entity";

@Entity()
export class Chat {
    @PrimaryGeneratedColumn()
    id: number;

    @ManyToMany(() => User)
    @JoinTable()
    users: User[];

    @OneToOne(() => Order, (order) => order.chat, {nullable: true})
    order: Order
}

服务层createChat方法

async createChat(userId, employeeId, orderId) {
        try {
            const candidateUser = await this.user.findOne({
                where: {
                    id: userId,
                },
            });

            const candidateEmployee = await this.user.findOne({
                where: {
                    id: employeeId,
                },
            });

            const candidateOrder = await this.order.findOne({
                where: {
                    id: orderId,
                },
            });

            if (candidateOrder && candidateUser && candidateEmployee) {
                const newChat = new Chat();
                newChat.users = [candidateEmployee];
                newChat.order = candidateOrder;

                const savedChat = await this.chat.save(newChat);

                candidateUser.currentChat = savedChat.id;
                candidateEmployee.currentChat = savedChat.id;

                await this.user.save(candidateUser);
                await this.user.save(candidateEmployee);

                candidateOrder.status = 'pending';

                await this.order.save(candidateOrder);

                return savedChat;
            } else {
                throw new Error('Не удалось создать чат.');
            }
        } catch (e) {
            throw new Error(e);
        }
    }

报错信息

[Nest] 25302  - 2023年10月7日 17:27:24   ERROR [ExceptionsHandler] UpdateValuesMissingError: 无法执行更新查询,因为未定义更新值。请调用"qb.set(...)"方法指定更新值。
Error: UpdateValuesMissingError: 无法执行更新查询,因为未定义更新值。请调用"qb.set(...)"方法指定更新值。
    在 ChatService.createChat (/mnt/hdd/web projects/telegram-monolith/telegram-server/src/chat/chat.service.ts:64:19)
    在 processTicksAndRejections (node:internal/process/task_queues:95:5)
    在 /mnt/hdd/web projects/telegram-monolith/telegram-server/node_modules/@nestjs/core/router/router-execution-context.js:46:28
    在 /mnt/hdd/web projects/telegram-monolith/telegram-server/node_modules/@nestjs/core/router/router-proxy.js:9:17
问题分析与解决办法

报错UpdateValuesMissingError说明TypeORM执行更新操作时未找到需更新的字段值,核心原因集中在用户实体的更新环节:

  1. 检查User实体的currentChat字段定义:确认User实体中是否存在currentChat字段,且用@Column()正确装饰(比如@Column({ nullable: true }))。如果字段未定义或映射错误,TypeORM会判定无更新内容,抛出此错误。

  2. 改用update()方法替代save():如果查询出的candidateUser/candidateEmployee脱离了TypeORM的实体管理(比如查询选项导致跟踪失效),调用save()时无法识别字段变更。直接用update()方法更可靠:

    // 替换原代码中的await this.user.save(...)
    await this.user.update(userId, { currentChat: savedChat.id });
    await this.user.update(employeeId, { currentChat: savedChat.id });
    
  3. 排除字段值未变更的情况:如果currentChat原本就等于savedChat.id(重复创建同一聊天场景),TypeORM不会生成更新语句,也可能触发错误。可先判断值是否变化再执行更新,或强制触发更新。

优先排查User实体的字段定义,若确认字段配置正确,改用update()方法是最直接的解决方案。

内容的提问来源于stack exchange,提问作者Potter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 23:50:09