evolution-api icon indicating copy to clipboard operation
evolution-api copied to clipboard

PrismaClientValidationError - Missing messageId in MessageUpdate creation

Open Davi-AMF opened this issue 1 year ago • 3 comments

Welcome!

  • [x] Yes, I have searched for similar issues on GitHub and found none.

What did you do?

When DATABASE_SAVE_MESSAGE_UPDATE=true is enabled, the application frequently fails to create MessageUpdate records due to a missing required messageId field, resulting in PrismaClientValidationError exceptions. The application attempts to create MessageUpdate records without providing the required messageId field, causing Prisma validation to fail. According to the database schema, messageId is a required (NOT NULL) field with a foreign key constraint to the Message table.

Table "MessageUpdate"
  messageId   text   NOT NULL

Foreign-key constraint:
  "MessageUpdate_messageId_fkey" FOREIGN KEY ("messageId")
    REFERENCES "Message"(id) ON DELETE CASCADE

Location: src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

  • Line 1508: Status update creation (first occurrence)
  • Line 1554: Status update creation (second occurrence)

Problem: The messageId field is added conditionally, causing failures when:

  • Database configuration does NOT have HISTORIC or NEW_MESSAGE enabled
  • Or when the parent message is not found in the database
// Lines 1479-1508 and 1551-1554
const message: any = {
  keyId: key.id,
  remoteJid: key?.remoteJid,
  fromMe: key.fromMe,
  participant: key?.remoteJid,
  status: status[update.status] ?? 'DELETED',
  pollUpdates,
  instanceId: this.instanceId,
  // ⚠️ messageId is NOT here initially
};

messageId is only added IF these conditions are true:

  if (configDatabaseData.HISTORIC || configDatabaseData.NEW_MESSAGE) {
    const messages = await this.prismaRepository.$queryRaw`
      SELECT id FROM "Message"
      WHERE "instanceId" = ${this.instanceId}
      AND "key"->>'id' = ${key.id}
      LIMIT 1
    `;
    findMessage = messages[0] || null;

    if (findMessage) message.messageId = findMessage.id; // ⚠️ Conditional!
  }

Both calls can fail if messageId is missing:

  await this.prismaRepository.messageUpdate.create({ data: message }); // Line 1508 ❌
  await this.prismaRepository.messageUpdate.create({ data: message }); // Line 1554 ❌

What did you expect?

The application should always provide a valid messageId when creating MessageUpdate records, or handle the case gracefully when the parent message doesn't exist.

What did you observe instead of what you expected?

The application throws PrismaClientValidationError: Argument 'Message' is missing when attempting to create MessageUpdate records. This occurs because the required messageId field is either conditionally added (only when specific config flags are enabled AND the parent message is found) or completely omitted (in the case of edited messages).

Screenshots/Videos

No response

Which version of the API are you using?

v2.3.4

What is your environment?

Docker

Other environment specifications

  • Prisma Client: 6.16.1
  • All 56 migrations are applied successfully. Database schema is up to date.
  • Configuration Tested
  DATABASE_PROVIDER=postgresql
  DATABASE_SAVE_DATA_NEW_MESSAGE=true
  DATABASE_SAVE_DATA_HISTORIC=true
  DATABASE_SAVE_MESSAGE_UPDATE=true

If applicable, paste the log output

 [Evolution API]    v2.3.4  261   -  Fri Oct 10 2025 14:02:07     ERROR   [unhandledRejection]  [object]
  PrismaClientValidationError:
  Invalid `this.prismaRepository.messageUpdate.create()` invocation in
  /evolution/dist/main.js:247:1169

    244   WHERE "instanceId" = ${this.instanceId}
    245   AND "key"->>'id' = ${i.id}
    246   LIMIT 1
  → 247 `)[0]||null,u&&(l.messageId=u.id)),n.message===null&&n.status===void
  0){this.sendDataWebhook("messages.delete",i),this.configService.get("DATABASE").SAVE_DATA.MESSAGE_UPDATE&&await
  this.prismaRepository.messageUpdate.create({
          data: {
            keyId: "3EB097CE6C9EB0568E0EC7",
            remoteJid: "5518997981710:[email protected]",
            fromMe: true,
            participant: "5518997981710:[email protected]",
            status: "DELIVERY_ACK",
            pollUpdates: undefined,
            instanceId: "12e9aef0-68d4-45de-af2f-1c9728a8468d",
        +   Message: {
        +     create: MessageCreateWithoutMessageUpdateInput | MessageUncheckedCreateWithoutMessageUpdateInput,
        +     connectOrCreate: MessageCreateOrConnectWithoutMessageUpdateInput,
        +     connect: MessageWhereUniqueInput
        +   }
          }
        })

  Argument `Message` is missing.
      at Nn (/evolution/node_modules/@prisma/client/runtime/library.js:29:1363)
      at ei.handleRequestError (/evolution/node_modules/@prisma/client/runtime/library.js:121:6911)
      at ei.handleAndLogRequestError (/evolution/node_modules/@prisma/client/runtime/library.js:121:6593)
      at ei.request (/evolution/node_modules/@prisma/client/runtime/library.js:121:6300)
      at async a (/evolution/node_modules/@prisma/client/runtime/library.js:130:9551)
      at async messages.update (/evolution/dist/main.js:247:1127) {
    clientVersion: '6.16.1'
  }
[Evolution API]    v2.3.4  261   -  Fri Oct 10 2025 14:02:10     ERROR   [unhandledRejection]  [object]
  PrismaClientValidationError:
  Invalid `this.prismaRepository.messageUpdate.create()` invocation in
  /evolution/dist/main.js:247:1169

    244   WHERE "instanceId" = ${this.instanceId}
    245   AND "key"->>'id' = ${i.id}
    246   LIMIT 1
  → 247 `)[0]||null,u&&(l.messageId=u.id)),n.message===null&&n.status===void
  0){this.sendDataWebhook("messages.delete",i),this.configService.get("DATABASE").SAVE_DATA.MESSAGE_UPDATE&&await
  this.prismaRepository.messageUpdate.create({
          data: {
            keyId: "A5DDF17E31AE63BE3FF3463F6D1D578F",
            remoteJid: "[email protected]",
            fromMe: false,
            participant: "[email protected]",
            status: "READ",
            pollUpdates: undefined,
            instanceId: "2c0806e5-c7b8-4069-bf05-c815864ecaa0",
        +   Message: {
        +     create: MessageCreateWithoutMessageUpdateInput | MessageUncheckedCreateWithoutMessageUpdateInput,
        +     connectOrCreate: MessageCreateOrConnectWithoutMessageUpdateInput,
        +     connect: MessageWhereUniqueInput
        +   }
          }
        })

  Argument `Message` is missing.
      at Nn (/evolution/node_modules/@prisma/client/runtime/library.js:29:1363)
      at ei.handleRequestError (/evolution/node_modules/@prisma/client/runtime/library.js:121:6911)
      at ei.handleAndLogRequestError (/evolution/node_modules/@prisma/client/runtime/library.js:121:6593)
      at ei.request (/evolution/node_modules/@prisma/client/runtime/library.js:121:6300)
      at async a (/evolution/node_modules/@prisma/client/runtime/library.js:130:9551)
      at async messages.update (/evolution/dist/main.js:247:1127) {
    clientVersion: '6.16.1'
  }

Additional Notes

No response

Davi-AMF avatar Oct 13 '25 13:10 Davi-AMF

Same here

forwardbridgefw-stack avatar Oct 14 '25 18:10 forwardbridgefw-stack

I set it to false (DATABASE_SAVE_MESSAGE_UPDATE) and still got the same issue sometimes.

The only thing that worked for me was restarting the application whenever it happened. But yeah, that’s definitely a bug and shouldn’t be happening.

guilhermegobbo avatar Nov 06 '25 17:11 guilhermegobbo

Olá! Estamos fazendo uma terceira rodada de triagem para manter o backlog focado em bugs reproduzíveis nas versões atuais.

Esta issue está sem atividade há vários meses e não houve confirmação de que o problema ainda existe nas versões atuais do Evolution API. No intervalo lançamos várias versões com mudanças significativas no Baileys, na camada de webhooks, na integração com Cloud API e em correções gerais.

Versões atuais:

  • Latest stable: v2.3.7 (Dezembro/2025)
  • Pre-release: 2.4.0-rc2 (lançada em Maio/2026) — recomendada para testes mais recentes

Vamos fechar esta issue por inatividade. Se você ainda reproduz o problema na 2.3.7 ou na 2.4.0-rc2, por favor abra uma nova issue com:

  1. Versão exata testada (2.4.0-rc2 preferencialmente)
  2. Passos para reproduzir
  3. Logs do servidor + payload de exemplo
  4. Comportamento esperado vs observado

Linke esta issue na nova para preservar o histórico. Obrigado pelo report original!

DavidsonGomes avatar May 19 '26 15:05 DavidsonGomes