RealmPrimaryKeyConstraintException有关更新Realm中的对象

时间:2018-10-23 18:04:44

标签: android realm realm-mobile-platform

我有以下型号:

购物车对象

open class Cart(
        @PrimaryKey
        var id: String? = null,

        var orders: RealmList<Deal> = RealmList()
) : RealmObject()

交易对象

open class Deal(

        @field:SerializedName("short_description")
        var shortDescription: String? = null,

        @field:SerializedName("original_price")
        var originalPrice: Int? = null,

        @field:SerializedName("deal_category")
        var dealCategory: DealCategory? = null,

        @field:SerializedName("images")
        var images: RealmList<Image>? = null,

        @field:SerializedName("featured")
        var featured: String? = null,

        @field:SerializedName("deal_type")
        var dealType: DealType? = null,

        @field:SerializedName("deal_of_day")
        var dealOfDay: String? = null,

        @field:SerializedName("restaurant")
        var restaurant: Restaurant? = null,

        @field:SerializedName("expiry_date")
        var expiryDate: String? = null,

        @field:SerializedName("description")
        var description: String? = null,

        @field:SerializedName("page_views")
        var pageViews: Int? = null,

        @field:SerializedName("price")
        var price: Int? = null,

        @field:SerializedName("name")
        var name: String? = null,

        @field:SerializedName("alias")
        var alias: String? = null,

        @field:SerializedName("links")
        var links: RealmList<Link>? = null,

        @field:SerializedName("id")
        var id: Int? = null,

        @field:SerializedName("user")
        var user: User? = null,

        @field:SerializedName("status")
        var status: String? = null
): RealmObject()

餐厅对象

open class Restaurant(

        @field:SerializedName("location_address")
        var locationAddress: String? = null,

        @field:SerializedName("website")
        var website: String? = null,

        @field:SerializedName("featured")
        var featured: String? = null,

        @field:SerializedName("latitude")
        var latitude: String? = null,

        @field:SerializedName("opening_time")
        var openingTime: String? = null,

        @field:SerializedName("description")
        var description: String? = null,

        @field:SerializedName("created_at")
        var createdAt: String? = null,

        @field:SerializedName("updated_at")
        var updatedAt: String? = null,

        @field:SerializedName("phone")
        var phone: String? = null,

        @field:SerializedName("closing_time")
        var closingTime: String? = null,

        @field:SerializedName("name")
        var name: String? = null,

        @field:SerializedName("preparation_time")
        var preparationTime: Int? = null,

        @field:SerializedName("alias")
        var alias: String? = null,

        @PrimaryKey
        @field:SerializedName("id")
        var id: Int? = null,

        @field:SerializedName("ideal_class")
        var idealClass: String? = null,

        @field:SerializedName("email")
        var email: String? = null,

        @field:SerializedName("status")
        var status: String? = null,

        @field:SerializedName("longitude")
        var longitude: String? = null
) : RealmObject()

当我最初将交易添加到购物车时,一切正常,但是当我将交易添加到具有现有餐厅条目的购物车时,我得到RealmPrimaryKeyConstraintException。我的方法如下:

Realm.getInstance(RealmUtil.realmConfiguration).use {realm ->
            var cart = realm.where<Cart>().findFirst()

            realm.executeTransaction {
                if (cart == null) {
                    cart = Cart().apply {
                        id = getString(R.string.app_name)
                        orders.add(deal)
                    }
                } else {
                    cart!!.orders.add(deal)
                }

                it.copyToRealmOrUpdate(cart!!)

                invalidateOptionsMenu()
            }
        }

我得到的错误是:

io.realm.exceptions.RealmPrimaryKeyConstraintException: Primary key value already exists: 43 .
    (/Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_OsObject.cpp:197)
        at io.realm.internal.OsObject.nativeCreateNewObjectWithLongPrimaryKey(Native Method)
        at io.realm.internal.OsObject.createWithPrimaryKey(OsObject.java:208)
        at io.realm.Realm.createObjectInternal(Realm.java:996)
        at io.realm.com_deals_bakooli_model_RestaurantRealmProxy.copy(com_deals_bakooli_model_RestaurantRealmProxy.java:1072)
        at io.realm.com_deals_bakooli_model_RestaurantRealmProxy.copyOrUpdate(com_deals_bakooli_model_RestaurantRealmProxy.java:1062)
        at io.realm.com_deals_bakooli_model_DealRealmProxy.copy(com_deals_bakooli_model_DealRealmProxy.java:1250)
        at io.realm.com_deals_bakooli_model_DealRealmProxy.copyOrUpdate(com_deals_bakooli_model_DealRealmProxy.java:1181)
        at io.realm.DefaultRealmModuleMediator.copyOrUpdate(DefaultRealmModuleMediator.java:214)
        at io.realm.Realm.copyOrUpdate(Realm.java:1634)
        at io.realm.Realm.copyToRealm(Realm.java:1016)
        at io.realm.RealmModelListOperator.copyToRealmIfNeeded(RealmList.java:1530)
        at io.realm.RealmModelListOperator.appendValue(RealmList.java:1456)
        at io.realm.ManagedListOperator.append(RealmList.java:1337)
        at io.realm.RealmList.add(RealmList.java:210)
        at com.deals.bakooli.activity.SingleDealActivity$addItemToCart$$inlined$use$lambda$1.execute(SingleDealActivity.kt:102)
        at io.realm.Realm.executeTransaction(Realm.java:1431)
        at com.deals.bakooli.activity.SingleDealActivity.addItemToCart(SingleDealActivity.kt:91)
        at com.deals.bakooli.activity.SingleDealActivity.onClick(SingleDealActivity.kt:82)

我不明白为什么即使使用copyToRealmOrUpdate也会引发异常。我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您的查询看起来不正确。

试试

realm.where(Cart.class).findFirst()