登录 主页

postgres 报错说:pq: 重复键违反唯一约束(pq: duplicate key value violates unique constraint)

2024-03-04 05:15PM

在动态排放平台创建新计算方案时,创建计算方案前端页面显示创建成功,但是计算方案的列表页面并没有新建的计算方案,并且数据库里面也没有新增计算方案的数据

console和前端页面的日志都没有报错,在后端日志中数据库有三个表报错说:pq: 重复键违反唯一约束

(/home/meiyi/workspace/dongtaipaifang_2_backend/models/calculation_result_detail.go:39) 
[2024-03-04 16:26:17]  pq: 重复键违反唯一约束"calculation_result_details_pkey" 

(/home/meiyi/workspace/dongtaipaifang_2_backend/models/calculation_plans.go:158) 
[2024-03-04 16:50:34]  pq: 重复键违反唯一约束"calculation_plans_pkey" 
(/home/meiyi/workspace/dongtaipaifang_2_backend/models/calculation_results.go:60) 
[2024-03-04 16:50:34]  pq: 重复键违反唯一约束"calculation_results_pkey" 

解决方法:https://stackoverflow.com/a/71904859

下面是我解决的这个问题的过程:

1. 在命令行里面进入postgres

meiyi@meiyi-Extensa-2511G:~/workspace/dongtaipaifang_2_backend$ sudo -u postgres -i
[sudo] password for meiyi: 
postgres@meiyi-Extensa-2511G:~$

2. 连接数据库

postgres@meiyi-Extensa-2511G:~$ psql -U postgres -d dongtaipaifang

postgres 是你的 PostgreSQL 用户名,dongtaipaifang  是你要连接的数据库名称

3. 列出数据库中的所有表 

dongtaipaifang=# \dt
                    List of relations
 Schema |            Name             | Type  |  Owner   
--------+-----------------------------+-------+----------
 public | calculation_plans           | table | postgres
 public | calculation_plans_materials | table | postgres
 public | calculation_result_details  | table | postgres
 public | calculation_results         | table | postgres
 public | calculation_templates       | table | postgres
 public | datasets                    | table | postgres
 public | history_login_logs          | table | postgres
 public | materials                   | table | postgres
 public | users                       | table | postgres
 public | verification_codes          | table | postgres
(10 rows)

4. 先找到报错的其中一个数据库表

dongtaipaifang=# \d calculation_result_details
                                            Table "public.calculation_result_details"
        Column         |           Type           | Collation | Nullable |                        Default                         
-----------------------+--------------------------+-----------+----------+--------------------------------------------------------
 id                    | integer                  |           | not null | nextval('calculation_result_details_id_seq'::regclass)
 calculation_result_id | integer                  |           |          | 
 material_id           | integer                  |           |          | 
 params                | text                     |           |          | 
 results               | text                     |           |          | 
 calculated_at         | timestamp with time zone |           |          | 
 created_at            | timestamp with time zone |           |          | 
 updated_at            | timestamp with time zone |           |          | 
Indexes:
    "calculation_result_details_pkey" PRIMARY KEY, btree (id)

 calculation_result_details 替换为实际的表名称。这将显示表的列名、数据类型和约束等详细信息

5.  给出表当前的最后一个 id

dongtaipaifang=# SELECT MAX(id) FROM calculation_result_details;  
 max  
------
 1738
(1 row)

 calculation_result_details 替换为你的表的实际名称

6. 根据postgresql获取下一个id序列。 确保此 id 高于我们从步骤 1 获得的当前最大 id

dongtaipaifang=# SELECT nextVal('"calculation_result_details_id_seq"');
 nextval 
---------
    1652
(1 row)

7. 如果不高于则使用步骤 3 更新下一个序列(我这里没有高于就使用了这个命令)

dongtaipaifang=# SELECT setval('"calculation_result_details_id_seq"', (SELECT MAX(id) FROM calculation_result_details)+1);
 setval 
--------
   1739
(1 row)

最后,把报错的剩下两个表(calculation_plans、calculation_results)也重复4~6的步骤,我这里就不写剩下两个报错的表了,然后在重新创建计算方案,就发现可以正常创建了,这个问题就解决了。

返回>>

登录

请登录后再发表评论。

评论列表:

目前还没有人发表评论