Skip to content
Blog cá nhân của PhongTV

Blog cá nhân của PhongTV

  • Home
  • About
  • Skills
  • Resume
  • Contact
  • Bài viết

How to fix error “Duplicate key value violates unique constraint PK_xxx” when insert data into Postgres table

pbk8 Tháng 4, 2024 No Comments

Firstly, run these 2 commands, be aware of Capitalize:

SELECT MAX("Id") FROM "TableName"; -- column Id is the primary key of table TableName, which is autoincrement
SELECT nextval(pg_get_serial_sequence('"TableName"', 'Id'));

For unknown reason, the result of the first query is greater then the second one. That cause the next inserted record will be duplicate with the exist value.

To fix that, just need to re-generate the auto increment value by this command:

SELECT setval(pg_get_serial_sequence('"TableName"', 'Id'), coalesce(max("Id") + 1, 1), false) FROM "TableName";

Điều hướng bài viết

Previous: Sửa lỗi System.PlatformNotSupportedException : System.Drawing.Common is not supported on non-Windows platforms. See https://aka.ms/systemdrawingnonwindows for more information trên Ubuntu
Next: How to fix error “Uncaught TypeError: ftp_fget(): Argument #1 ($ftp) must be of type FTP\Connection, null given in class-wp-filesystem-ftpext.php” in WordPress

Để lại một bình luận Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Proudly powered by WordPress | Theme: Smart Portfolio by Code Work Web.
Contact Us

    This form is powered by: Sticky Floating Forms Lite