title: RealmPrimaryKeyConstraintException on updating objects in Realm
body: <p>I have the following models:</p>
  
  <p><strong>Cart object</strong></p>
  
  <pre><code>open class Cart(
          @PrimaryKey
          var id: String? = null,
  
          var orders: RealmList&lt;Deal&gt; = RealmList()
  ) : RealmObject()
  </code></pre>
  
  <p><strong>Deal object</strong></p>
  
  <pre><code>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&lt;Image&gt;? = 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&lt;Link&gt;? = null,
  
          @field:SerializedName("id")
          var id: Int? = null,
  
          @field:SerializedName("user")
          var user: User? = null,
  
          @field:SerializedName("status")
          var status: String? = null
  ): RealmObject()
  </code></pre>
  
  <p><strong>Restaurant object</strong></p>
  
  <pre><code>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()
  </code></pre>
  
  <p>When I initially add deals to the cart, everything functions ok but when I add a deal to the cart that has an already existing restaurant entry present, I get the <code>RealmPrimaryKeyConstraintException</code>. My method is as below:</p>
  
  <pre><code>Realm.getInstance(RealmUtil.realmConfiguration).use {realm -&gt;
              var cart = realm.where&lt;Cart&gt;().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()
              }
          }
  </code></pre>
  
  <p>The error I get is:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I don't understand why despite using <code>copyToRealmOrUpdate</code>, the exception is thrown. What am I doing wrong?</p>