Replies: 2 comments
-
|
Are you trying to update a unique key(the primary key) based on a conflict on another unique key? 🤯 I don't think you can satisfy both constraints at the same time on a single ON CONFLICT statement. That would also fail with pure SQL: CREATE TABLE board_cards (
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
txt text,
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
preceded_by uuid REFERENCES board_cards(id) UNIQUE
);
INSERT INTO board_cards (txt, id, preceded_by) VALUES
('A1', '862dcfa0-0c07-44c6-b8d0-8f2b5c4365e7', null),
('A2', 'bf3f21a0-71ac-4f94-988f-f63a354979f7', '862dcfa0-0c07-44c6-b8d0-8f2b5c4365e7'),
('A3', '6ed862aa-076e-44ae-8db1-67a3386efe8d', 'bf3f21a0-71ac-4f94-988f-f63a354979f7');
-- this is essentially what happens when doing "upsert" on supabase-js
INSERT INTO board_cards (id, preceded_by) VALUES
('bf3f21a0-71ac-4f94-988f-f63a354979f7', null),
('862dcfa0-0c07-44c6-b8d0-8f2b5c4365e7', 'bf3f21a0-71ac-4f94-988f-f63a354979f7'),
('6ed862aa-076e-44ae-8db1-67a3386efe8d', '862dcfa0-0c07-44c6-b8d0-8f2b5c4365e7')
ON CONFLICT(preceded_by) DO UPDATE SET id = EXCLUDED.id;
ERROR: 23505: duplicate key value violates unique constraint "board_cards_pkey"
DETAIL: Key (id)=(bf3f21a0-71ac-4f94-988f-f63a354979f7) already exists.
SCHEMA NAME: test
TABLE NAME: board_cards
CONSTRAINT NAME: board_cards_pkeySorry, I must say I'm confused by the database model. At this point I'd suggest doing the custom logic on a function + rpc, or perhaps rework the model if possible, so you can do the ON CONFLICT on the primary key. |
Beta Was this translation helpful? Give feedback.
-
|
I think i'm kinda late, but imma try. I had the same problem, and the only way that worked for me to fix the problem, was to remove the foreing key relation and setting the id-colunm as a not primary key, hope this helps other people with the same problem. pd: If you're gonna insert more than just one row with the same id, you're gonna have this problem, use the primary key just if you're gonna insert one row using the foreing key relation or the normal id, otherwise, you're gonna receive the message in the comments above. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm running into the above issue when trying to do a bulk-update (upsert) of multiple existing rows.
I have the following table and data:
The above represents a "card" in a singly-linked list (think something like Trello lists where cards are sequentially ordered). the
preceded_byfield references theboard_card.idthat precedes the current card.I am calling this in supabase:
However this produces the following error message:
I've tried setting:
However that doesn't work either:
any input would be much appreciated! I'm sure it's something simple I am missing.
Beta Was this translation helpful? Give feedback.
All reactions