How to check if a Row exist in Supabase?

Describe the problem/error/question

Hi,

I am a new user and have self hosted n8n on Railway.app platform.

This is my first query.

I am building a workflow in which I want to fetch data from an API which is returned in JSON format and then save this data to a database.

I am trying to check in Supabase if a row exist containing a unique field value or not.

What I want to do is: If the row does not exist then I want to add it and if it exist then I want to update it.

How to do that?

TIA

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version: Latest
  • Database (default: SQLite): Supabase
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Main
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Railway
  • Operating system: Don’t know

Hi @YogiYang, welcome to the community!

I think the cleanest way would be to use the PostgreSQL node. Supabase allows direct connections to the PostgreSQL database, with the credentials being available under Project Settings → Database.

Assuming my Supabase table looks like this:

And my n8n data looks like so:

image

I could then run a simple SQL query telling me if a row exists or not, for example like so:

In a workflow this approach would look like so, I’ve also added an IF node to process the result of the SQL query:

Hope this helps!

Btw, this isn’t the only option, just how I’d personally approach such a workflow for best readability. Instead of the query above and depending on how your table is set up you could for example write an UPSERT query (and wouldn’t have to implement the IF part of the example above).

You could also use the Supabase node with the Row → Get Many operation to see if there’s a match for your existing data. You would need to break down your data into batches of 1 in this case though, and build a looping logic similar to what I did here for Google Contacts.

2 Likes

Thank you.

I will try this out and get back if I run into any problems.

1 Like

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