Combination of Edit Fields, Code, Postgres nodes results in strange behaviour

Describe the problem/error/question

Hi all, I encountered strange behavior when I was implementing a Webhook Workflow. I want to implement a REST API endpoint PATCH /submissions/:id where I can update the status and external_id of a submission. I want to be able to update one attribute at a time or all at once, for example, update only status, update only external_id, or update both.

The issue I encountered is with the Code node, where I want to prepare the columns for the Update operation of the Postgres node, but somehow I seem to see one output on the UI but it is passed differently to the Postgres node.

This is the database structure:

create type test.submission_status as enum ('draft', 'published', 'invalid');

create table test.submissions
(
    id                          integer generated by default as identity        primary key,
    status                      test.submission_status     default 'draft'::test.submission_status              not null,
    external_id                 text,
    created_at                  timestamp      default CURRENT_TIMESTAMP                not null,
    updated_at                  timestamp      default CURRENT_TIMESTAMP                not null
)

Here is the curl of the request:

curl --request PATCH \
  --url http://localhost:5678/webhook-test/140315f6-6ee3-4398-9066-54d6be601fa0/submissions/1 \
  --header 'Content-Type: application/json' \
  --data '{
  "data": {
    "attributes": {
      "external_id": "R_123"
    }
  }
}'

The Working Code Node and Failing Code Node have the same output on the UI, but the Postgres node is failing for Failing Code Node because it is trying to update all the fields instead of just the changed one.

What is the error message (if any)?

When I update only the external_id, I receive:

null value in column "status" of relation "submissions" violates not-null constraint
Failing row contains (1, null, R_123, 2025-01-11 11:41:18.075582, 2025-01-11 11:41:18.075582).

Please share your workflow

Is there something wrong with the Code node? Am I missing something on Javascript coding? Is there something wrong with the Postgres node?

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.69.2
  • Database (default: SQLite): postgres 17.2
  • n8n EXECUTIONS_PROCESS setting (default: own, main): default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • Operating system: MacOS
2 Likes

Hey @pdany1116

I’ve tried out the the workflow you provided and replicated your database and based on the example data in your flow it seems to work for me with both the working and failing Code Node.

I think there is probably a good reason for this and that is mainly down to copying the JSON of the workflow into the canvas from here is not giving the whole story - for example, do you have any fields at all in the ‘Fields to Set’ section of the Edit Fields Node or is that empty and you only have the Include Other Input Fields slider activated and then the your items in Fields to Include, or is there something in the Fields to Set section that is not copying over?

The example that you provided it only resulting in the following JSON from both of the Code Nodes:

[
  {
    "id": "1",
    "external_id": "R_123"
  }
]

And because there is no Status in this, it works - and you’re right that both the working and failing Code node returns the same output.

Without knowing if the set node is doing anything else or what the other parts of the logic are, I can’t replicate it. But, if the code node in the full logic has Status in it and that is either set to blank or null, it will fail as the database schema expects something in that filed - it’s set to not allow null, meaning the Postgres node will attempt to update it if it’s included in the incoming data to that node - in other words it will match the field if it’s included - but I may well be telling you something you already know here and if that is the case I apologise.

So that data coming into the Postgres node will either need Status with a value or Status removed completely if it is a null value but based on the workflow example, I can’t see where Status is coming into the equation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.