Cause: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "veh

插入数据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 序列表如下
-- Sequence: "vehicle__appKey_id_seq"
-- DROP SEQUENCE "vehicle__appKey_id_seq";

CREATE SEQUENCE "vehicle__appKey_id_seq"
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 711
  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的最大值

Cause: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint

你可能感兴趣的:(java)