1

I have been using a play framework rest api for a couple of months now hosted on heroku and using the underlying postgres db. All of a sudden, I started getting the following error today

Execution exception[[PersistenceException: ERROR executing DML bindLog[] error[ERROR: duplicate key value violates unique constraint "pk_informal_sector_waste_composition"\n Detail: Key (id)=(1366) already exists.

Heroku support suspected index corruption, so I followed the steps outlined here How to reset postgres' primary key sequence when it falls out of sync?.

However, this did not help. When I checked the table causing the issue, I did find the ID mentioned above.

My understanding is that based on the database, ebean knows what kind of sequence generator to use for the ID field (annotated with @Id).

Is it possible that ebean is causing this issue? It's puzzling because everything worked ok for the last couple of months and there have been no code changes since.

Below are my model objects:

@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;

}
  .....
  .....
}

Greatly appreciate any insights the community can provide.

Thanks, RK

Community
  • 1
  • 1
  • Sounds as if someone manually inserted that key value into the table without using the sequence. Or the sequence was set manually to a different value. Check the sequence values using `nextval()` and if it is wrong use `setval()` to adjust it –  May 24 '14 at 06:35
  • A sequence "falling out of sync" from the values in a table is *not* index corruption. The two things are totally unrelated. – Craig Ringer May 24 '14 at 07:21
  • Hey did you solve this problem in the end? we are running the same play framework version on heroku. – mike james Apr 02 '15 at 16:10

0 Answers0