插入数据postgre 报错如下
### Error updating database. Cause: org.postgresql.util.PsqlException: ERROR: duplicate key value violates unique constraint "vehicle__appKey_pkey" ### The error may involve com.sq.platform.basePlatform.dao.VehicleMapper.insertVehicleAppkey-Inline ### The error occurred while setting parameters ### sql: insert into vehicle__appkey(vehicle_id_fk,appkey_id_fk) values (?,?) ### Cause: org.postgresql.util.PsqlException: ERROR: duplicate key value violates unique constraint "vehicle__appKey_pkey" ; sql []; ERROR: duplicate key value violates unique constraint "vehicle__appKey_pkey"; nested exception is org.postgresql.util.PsqlException: ERROR: duplicate key value violates unique constraint "vehicle__appKey_pkey" at org.springframework.jdbc.support.sqlErrorCodesqlExceptionTranslator.doTranslate(sqlErrorCodesqlExceptionTranslator.java:239) at org.springframework.jdbc.support.AbstractFallbacksqlExceptionTranslator.translate(AbstractFallbacksqlExceptionTranslator.java:73) at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:74) at org.mybatis.spring.sqlSessionTemplate$sqlSessionInterceptor.invoke(sqlSessionTemplate.java:399) at com.sun.proxy.$Proxy11.insert(Unknown Source)
表中 vehicle__appkey id字段如下
id integer NOT NULL DEFAULT nextval('"vehicle__appKey_id_seq"'::regclass),
vehicle__appKey_pkey 序列表如下
<span style="font-family: Arial,Helvetica,sans-serif;">-- Sequence: "vehicle__appKey_id_seq"</span>
<pre name="code" class="java"> -- DROP SEQUENCE "vehicle__appKey_id_seq"; CREATE SEQUENCE "vehicle__appKey_id_seq" INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807 <span style="color:#ff0000;">START 711</span> CACHE 1; ALTER TABLE "vehicle__appKey_id_seq" OWNER TO postgres;
不难看出以上错误是由于 vehicle__appkey中id字段重复导致的,而id字段的值来自于 vehicle__appKey_pkey 序列表, START 711 标示下一个id值为712,而712的id vehicle__appkey表中已经存在了,所以插入数据时报以上这个错误
决绝方法:将"vehicle__appKey_id_seq"中的当前值设为为vehicle__appkey id的最大值