title: Realm - Can't create object with existing primary key value
body: <p>I have a object Person with many dogs. App has separate page where it shows just dogs and other page where it shows person's dogs     </p>
  
  <p>My model is as follows</p>
  
  <pre><code>class Person: Object {
      dynamic var id = 0
      let dogs= List&lt;Dog&gt;()
  
      override static func primaryKey() -&gt; String? {
          return "id"
      }
  }
  
  class Dog: Object {
      dynamic var id = 0
      dynamic var name = ""
  
      override static func primaryKey() -&gt; String? {
          return "id"
      }
  }
  </code></pre>
  
  <p>I have persons stored in Realm. Person has detail page where we fetch and show his dogs. If dog already exist, I update latest info for that dog and add it to person's dog list else create new dog, save it and add it to persons list. This works in coredata. </p>
  
  <pre><code>// Fetch and parse dogs
  if let person = realm.objects(Person.self).filter("id =\(personID)").first {
      for (_, dict): (String, JSON) in response {
          // Create dog using the dict info,my custom init method
          if let dog = Dog(dict: dict) {
              try! realm.write {
                  // save it to realm
                  realm.create(Dog, value:dog, update: true)
                  // append dog to person
                  person.dogs.append(dog)
              }
          }
      }
      try! realm.write {
          // save person
          realm.create(Person.self, value: person, update: true)
      }
  }
  </code></pre>
  
  <p>On trying to update person with his dogs,realm throws exception
  <strong>Can't create object with existing primary key value</strong> </p>