ERROR: Duplicate key value violates unique constraint "boards_pkey" error failed to investigate.

Asked 1 years ago, Updated 1 years ago, 76 views

https://github.com/blobmon/simplechan
After completing the above site and entering the database to change the name of board1 displayed on the site to "Anime&Manga",

simplech_db-# update boards set board='Anime&Manga', display_name='Anime&Manga'where board='board1' and display_name='board1';

I entered

ERROR: duplicate key value violates unique constraint "boards_pkey"

DETAIL: Key (board) = (Anime & Manga) already exists.

is displayed and cannot be displayed as .
I'm going to turn it off again.

truncate table boards;

If you type

ERROR:cannot truncate a table referred in a foreign key constraint
DETAIL: Table "posts" references "boards".
HINT: Truncate table "posts" at the same time, or use TRUNCATE...CASCADE.

appears.How can I do it?

linux ubuntu postgresql

2022-09-30 21:32

2 Answers

First, understand the message.

ERROR: duplicate key value violates unique constraint "boards_pkey"
Duplicate key values violate the constraint that the main key in the boards table must be unique.
"DETAIL: Key (board) = (Anime & Manga) already exists."
A record with the value "Anime&Manga" on the keyboard already exists.

"How can I do this?"
Delete any records that already have a board value of "Anime&Manga".
This way, changing the board value to "Anime&Manga" will not cause duplicate keys.


2022-09-30 21:32

How can I do it?

The meaning of the message below is

ERROR: duplicate key value violates unique constraint "boards_pkey"
DETAIL: Key (board) = (Anime & Manga) already exists.

In the boards table, the board column is the primary key and the same value is prohibited.
"Anime & Manga" is set in the board column of the existing row in the boards table, so I think it means there is an error.

Also, the meaning of the message below is

ERROR:cannot truncate a table referred in a foreign key constraint
DETAIL: Table "posts" references "boards".
HINT: Truncate table "posts" at the same time, or use TRUNCATE...CASCADE.

The data in the posts table referencing the boards table cannot be truncated.
If you want to trunk the posts table at the same time as the boards, I think it means to use TRUNCATE TABLE boards CASCADE.

I didn't know, but according to the PostgreSQL documentation,
"TRUNCATE does not issue any ON DELETE triggers in the table." That's right.

Therefore,
(a) If you want to delete all data from the tables associated with the boards table, use the following:

TRUNCATE TABLE boards CASCADE;

(A) If you want to delete only the existing line Anime&Manga, you can use the following:
(Related tables are set to ON DELETE CASCADE, so related data should be deleted as well.)

 DELETE FROM boards WHERE board = 'Anime & Manga'

(a) After you do one of the following, you should be able to do the following:

update boards set board='Anime&Manga', display_name='Anime&Manga'where board='board1' and display_name='board1';

Good luck with your investigation.


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.