Search code examples
nestjstypeorm

NestJS creates wrong query (duplicated parameters)


NestJS using typeORM is creating SQL query with duplicated parameter, and fails the call.

The big mystery is that two different nodes with the same code behave differently (each time only one of them is active, and always called from the same local client), one is consistently failing, while the other works as expected.

The nodeJS failing call is

await this.profileRepository.update({ id }, data);

The client, when routed to the failing node, returns this error:

ApiException - Error calling SetMetadataPost: 

{
  "message": "multiple assignments to same column \"project_id\"",
  "length": 121,
  "name": "QueryFailedError",
  "severity": "ERROR",
  "code": "42601",
  "file": "rewriteHandler.c",
  "line": "1061",
  "routine": "process_matched_tle",
  "query": "UPDATE \"profile\" SET \"id\" = $2, \"created_at\" = $3, \"updated_at\" = $4, \"name\" = $5, \"is_default\" = $6, \"profile_data\" = $7, \"result\" = $8, \"project_id\" = $9, \"status\" = $10, \"stage\" = $11, \"markups\" = $12, \"parent_profile_id\" = $13, \"project_id\" = $14 WHERE \"id\" = $1",
  "parameters": [
    223060,
    223060,
    "2023-10-01T10:54:00.468Z",
    "2023-10-01T10:58:29.465Z",
    "Untitled project 2",
    false,
    "{}",
    "{\"name\":\"name1\"}",
    9729,
    "READY",
    "S1",
    "{}",
    223059
  ],
  "timestamp": "2023-10-01T10:58:29.552Z",
  "path": "/api/v1/prof/223060/set-metadata"
}

Solution

  • I found out what the problem is from this discussion here: https://github.com/typeorm/typeorm/issues/2651

    An answer from @evgenytk helped me to get to the solution.

    delete data.project;
    await this.projectProfileRepository.update({ id }, data);
    

    project is a linked-by-column property, so updating with it made a duplicated columns issue. By removing it, the problem was fixed.