# How to avoid duplicate ID error when inserting

**URL:** <https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659>\
**Category:** Ask us anything\
**Tags:** supabase\
**Created:** [November 30, 2023, 7:41pm UTC](https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659 "2023-11-30T19:41:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Takako](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Takako](https://community.weweb.io/u/Takako)\
**Post date:** [November 30, 2023, 7:41pm UTC](https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659/1 "2023-11-30T19:41:37Z")

</div>

I’m having an issue when I try to insert a new row to a Supabase table from WeWeb workflows.

1. One of my Supabase tables has 1521 records, which are imported from a different database.
2. The id (primary key) of records are not completely consistent, some numbers are lost as some data were deleted before. (e.g., the first row starts with id 79, the last row has id 2032)
3. When I try to insert a new row to the table from WeWeb using “Insert” type action in the Workflow, it tries to add a new row with id “1”. As I also already added an entry with id 1 for testing, it generates an error.  
`"duplicate key value violates unique constraint "[tableName]_pkey""`

My question is, how can I add a new row with an autoincremented id based on my Supabase table?  
I thought of getting the whole rows + checking the id of the last row, but since I added RLS policy on Supabase to limit users to get only the user’s own data, I can’t get the whole table data as a user.

Sorry if my question is confusing, please let me know if anything is unclear.  
Hoping any help.

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [November 30, 2023, 7:43pm UTC](https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659/2 "2023-11-30T19:43:59Z")

</div>

Do you have the auto increment set up in your Supabase table? Or where does the ID, that is conflicting coming from? Could you show us the setup of your Supabase Action? Just to be sure we get the context.

---

<div class="post-metadata">

**Author:** ![Takako](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Takako](https://community.weweb.io/u/Takako)\
**Post date:** [November 30, 2023, 8:03pm UTC](https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659/3 "2023-11-30T20:03:50Z")

</div>

Hi Broberto, thank you for the reply,

Supabase setting:

 ![Screenshot 2023-11-30 at 11.48.38 AM](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/2/27847f2bb6fe95425dde9813a4c73ccafdf9890c.png)

Supabase action on WeWeb:

 ![Screenshot 2023-11-30 at 11.59.14 AM](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/9/956c768239993670e51207a7d7803b51b540cef4.png)

I also run  
`SELECT setval('[tableName]_id_seq', (SELECT MAX(id) FROM "[tableName]")+1);` on Supabase:  
 ![Screenshot 2023-11-30 at 11.44.52 AM](https://us1.discourse-cdn.com/flex016/uploads/weweb/original/2X/3/315d33714e22912fd8ceb3d7c66bad11f18be745.png)

Tried to find the value of id it tries to insert, but couldn’t find in Supabase logs, browser console and the network tab

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [November 30, 2023, 8:13pm UTC](https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659/4 "2023-11-30T20:13:18Z")

</div>

I’m not sure about this one. But I did something similar with triggers for mixed values in Supabase upsert. What I did was, I set up a trigger before insert that would generate the id (if it was null).

---

<div class="post-metadata">

**Author:** ![Takako](https://avatars.discourse-cdn.com/v4/letter/t/4491bb/32.png) [@Takako](https://community.weweb.io/u/Takako)\
**Post date:** [November 30, 2023, 9:05pm UTC](https://community.weweb.io/t/how-to-avoid-duplicate-id-error-when-inserting/5659/5 "2023-11-30T21:05:47Z")

</div>

Sorry, I was looking into again after your reply to try triggers and functions,  
and found that it seems simply about my configuration.

I run

```auto
ALTER TABLE <table_name> 
    ALTER COLUMN id 
    RESTART WITH <id_value_you_want_to_start_from>;

```

and it solved the id issue.

Thanks regardless for checking my question 🙏
