Data table rows - execution stops if they do not exist

Describe the problem/error/question

Scenario (simplified in order to demo functionality): I want to keep a tally of the number of times some event happens. I have a number of workflows logging an event into a “Queue” data table. In this case this I’m representing these events as a simple string “a”, “b”, “c” etc.

On a schedule another workflow pulls all rows from this “Queue” table (column: “KeyToAdd”) and attempts to increase a running count in a “Counter” table (columns: Key, Count”)

Initial condition:

Queue table contains rows “a”, “b”, “c”, “a”.

Count table contains “a, 1”, “c, 1”, “d, 1”. NO B ROW

Workflow first fetches all rows from queue - result is 4 items

Then performs an aggregation, result is 3 items for the “extra count” of each key (a: 2 extra, b: 1 extra, c: 1 extra)

Next fetch the row with the key from Counter table (in order to get the current count).

Then Upsert the row using {current count + extra count}

The problem: because key “b” does not exist in the counter table, the GET node doesnt return when evalution the b key so the upsert node never runs for the b aggregate item

final result on the counter is a:3, c:2, d:1

So I tried a second approach that uses the “if row exists/doesnt exist” filter nodes, however this creates a race condition

3 items from the aggregator node are passed to each branch,

“if row doesnt exist”, only 1 item passes (for b), then use a set node to create a fake value, which then gets passed to the upsert correctly.

However, then the “if row exists” branch executes, and of the 3 input items, all 3 succeed, because the flow just created the b row, and it adds double the extra count

final result on the counter is a:3, b:2, c:2, d:1

I wouldnt want to assume that the “doesn’t exist” path will always run first

I feel like this would be solved by a node “Does row exist” with true / false outputs, then only one path can execute for each item tested, i can’t figure out how to simulate this, forexample doing a “get row“ with a non-existent key doesnt return anything - the output (empty) item is removed so subsequent nodes cant run to test for null (input 3 items > output 2 items)

Both test flows to demo this:

Please share your workflow

Information on your n8n setup

  • n8n version: 1.121.3 (Latest Stable) on n8n cloud

You must add a Merge node before Upsert Rows node. by adding merge node first both branches execute and then upsert rows will be executed so no race condition happens

1 Like

Oh perfect, nice simple fix.

Thanks ramin1!

1 Like

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