Skip to content

UniqueConstraintException when upserting #193

@lahiruchandima

Description

@lahiruchandima

I have below data model.


@Indices({@Index(value = "synced", type = IndexType.NonUnique)})
public class Receipt implements Parcelable {
    @Id
    public String clientRef;
    public String dailySequence;
    public double discount;
    public double serviceCharge;
    public String steward;
    public ReceiptItem[] items;
    public boolean synced;

    // methods .....
}

And I save objects of this model to nitrite like below.

    receiptRepository = nitriteDb.getRepository(Receipt.class);

    public void persistReceipt(Receipt receipt) {
        receiptRepository.update(receipt, true);
    }

Infrequently, I get below exception when updating a receipt using above code. Note that I am upserting the receipt, and there is only a single unique key. So I am not expecting a unique constraint violation, even though the db already has a receipt with the same clientRef. If receipt already found, it should be updated because I specified to upsert.

Fatal Exception: org.dizitart.no2.exceptions.UniqueConstraintException: NO2.10003: unique key constraint violation for clientRef
       at org.dizitart.no2.internals.IndexingService.updateIndexEntry(IndexingService.java:126)
       at org.dizitart.no2.internals.DataService.insert(DataService.java:94)
       at org.dizitart.no2.internals.DataService.update(DataService.java:134)
       at org.dizitart.no2.internals.NitriteService.update(NitriteService.java:361)
       at org.dizitart.no2.internals.DefaultNitriteCollection.update(DefaultNitriteCollection.java:322)
       at org.dizitart.no2.objects.DefaultObjectRepository.update(DefaultObjectRepository.java:141)
       at org.dizitart.no2.objects.DefaultObjectRepository.update(DefaultObjectRepository.java:126)
       at com.lahiruchandima.pos.core.DatabaseHelper.persistReceipt(DatabaseHelper.java:483)
       at com.lahiruchandima.pos.core.ReceiptCacher.lambda$null$5$ReceiptCacher(ReceiptCacher.java:109)
       at com.lahiruchandima.pos.core.-$$Lambda$ReceiptCacher$FaO2Rq30Et4o2YAiQFhLGXUb3DI.run(lambda)
       at java.lang.Thread.run(Thread.java:776)

Can this be a bug in nitrite? Is there any workaround for this?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions