我一直在使用一个播放框架rest api几个月,现在托管在heroku上并使用底层的postgres db。突然间,我今天开始收到以下错误
执行异常[[PersistenceException:ERROR正在执行DML bindLog []错误[错误:重复键值违反了唯一约束" pk_informal_sector_waste_composition" \ n详细信息:Key(id)=(1366)已经存在。
Heroku支持可疑的索引损坏,因此我按照此处列出的步骤进行了How to reset postgres' primary key sequence when it falls out of sync?。
然而,这没有帮助。当我检查表格导致问题时,我确实找到了上面提到的ID。
我的理解是,基于数据库,ebean知道用于ID字段的序列生成器是什么类型(用@Id注释)。
ebean是否可能导致此问题?令人费解的是,因为过去几个月一切正常,而且自那以后没有任何代码更改。
以下是我的模型对象:
@Entity
public class InformalSectorWasteComposition extends Model {
@Id
public String id;
.......
.......
@ManyToOne
@JsonBackReference
public InformalSector informalSector;
public static String create(InformalSectorWasteComposition wc) {
//TODO: check if lead exists and update...
wc.save();
return wc.id;
}
}
@Entity
public class InformalSector extends Model {
@Id
public String id;
@OneToMany(cascade=CascadeType.ALL) //one lead can have many wcData
@JsonManagedReference
public List<InformalSectorWasteComposition> wcData;
public static String create(InformalSector informalSector) {
//TODO: Check if lead exists...if so update
Long id = -1L;
try{
id = Long.parseLong(informalSector.id);
}
catch (Exception e){
e.printStackTrace();
}
if (id == -1){
//new lead
informalSector.save();
return informalSector.id;
}
else{ //existing
InformalSector current = InformalSector.get(id);
if (current != null){
Logger.debug(current.id);
current = informalSector;
current.update();
return current.id;
}
}
return null;
}
.....
.....
}
非常感谢社区提供的任何见解。
谢谢, RK