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

Prisma+BunJS测试时prisma.user.create()出现ID识别错误

问题总结

在基于BunJS + Prisma(PostgreSQL)的项目中,使用cucumberjs和keploy执行接口测试时,调用POST接口(如signup、create-post)触发prisma.user.create()操作时出现查询引擎panic错误:

Invalid prisma.user.create() invocation: Could not figure out an ID in create. This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

项目正常运行时无此异常,环境配置如下:

  • Node v21.6.0
  • linux-arm64-openssl-3.0.x系统
  • Prisma Client 5.12.1+(已升级至最新版本)
  • PostgreSQL数据库
  • User模型id为自增Int类型

已尝试bunx prisma format、validate、migrate dev、generate等命令,问题未解决;忽略POST调用时测试正常。

解决方案
  • 验证测试数据库表结构
    确认测试环境的User表id字段已正确设置为PostgreSQL自增类型(SERIAL),执行以下SQL查询验证:

    SELECT column_default FROM information_schema.columns WHERE table_name = "User" AND column_name = "id";
    

    若返回值不为序列生成逻辑,需重新执行迁移确保测试库与开发库结构一致,执行:bunx prisma migrate reset --env-file .env.test(指定测试环境配置文件)。

  • 排查Keploy的mock干扰
    Keploy在录制/回放过程中可能篡改数据或请求上下文,尝试临时禁用Keploy,仅用cucumberjs运行POST测试,确认是否为Keploy与Prisma的兼容性问题。若禁用后恢复正常,需检查Keploy的录制规则,排除对user.create()操作的异常拦截或参数篡改。

  • 修复Prisma引擎适配问题
    由于运行在linux-arm64架构,手动清理并重新获取适配的查询引擎:

    1. 删除node_modules/@prisma/engines目录
    2. 执行bunx prisma generate --schema ./prisma/schema.prisma(明确指定schema路径)
    3. 若仍有问题,尝试降级Prisma至5.11.x版本,部分新版本在arm64环境存在自增ID处理的临时bug。
  • 规范测试代码的create操作
    确保测试用例中调用prisma.user.create()时,未主动传入id字段(即使值为undefined或null),避免干扰Prisma的自增逻辑。正确示例:

    await prisma.user.create({
      data: {
        username: "test_user",
        email: "test@example.com"
        // 不包含id字段,由数据库自增生成
      }
    });
    
  • 开启日志定位panic细节
    启动Prisma debug日志,执行测试时添加环境变量:

    PRISMA_LOG_LEVEL=debug bun run your-test-command
    

    通过日志中的panic堆栈信息,定位引擎处理自增ID时的具体错误环节,便于针对性修复。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 04:58:15