{"action":"created","issue":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/2331","labels_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2331/labels{/name}","comments_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2331/comments","events_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2331/events","html_url":"https://github.com/realm/realm-cocoa/issues/2331","id":98611082,"number":2331,"title":"Question: Update/Set relationship without updating existing child object","user":{"login":"schickling","id":1567498,"avatar_url":"https://avatars.githubusercontent.com/u/1567498?v=3","gravatar_id":"","url":"https://api.github.com/users/schickling","html_url":"https://github.com/schickling","followers_url":"https://api.github.com/users/schickling/followers","following_url":"https://api.github.com/users/schickling/following{/other_user}","gists_url":"https://api.github.com/users/schickling/gists{/gist_id}","starred_url":"https://api.github.com/users/schickling/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/schickling/subscriptions","organizations_url":"https://api.github.com/users/schickling/orgs","repos_url":"https://api.github.com/users/schickling/repos","events_url":"https://api.github.com/users/schickling/events{/privacy}","received_events_url":"https://api.github.com/users/schickling/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-02T14:08:10Z","updated_at":"2015-08-02T17:42:37Z","closed_at":null,"body":"Given the following data model, is there a way to update or set a child relationship without updating the (existing) child object?\r\n\r\n**Data Model**\r\n```swift\r\nclass Person: Object {\r\n    dynamic var id = 0\r\n    dynamic var dog: Dog?\r\n    \r\n    override static func primaryKey() -> String? {\r\n        return \"id\"\r\n    }\r\n}\r\n\r\nclass Dog: Object {\r\n    dynamic var id = 0\r\n    dynamic var name = \"\"\r\n    \r\n    override static func primaryKey() -> String? {\r\n        return \"id\"\r\n    }\r\n}\r\n```\r\n\r\n**Code**\r\n```swift\r\nlet realm = Realm()\r\n\r\n// query existing dog\r\nlet existingDog = realm.objectForPrimaryKey(Dog.self, key: 42)\r\nprint(existingDog) // id: 42, name: Rex\r\n\r\nlet newDog = Dog(value: [\"id\": 42, \"name\": \"Pluto\"])\r\nlet person = Person()\r\n\r\n\r\nrealm.write {\r\n    // this shouldn't update the existing dog\r\n    // i.e. the name of the dog with id: 42 in the database should still be \"Rex\"\r\n    person.dog = newDog\r\n    realm.add(person, update: true)\r\n}\r\n\r\n```\r\nI've also tried to do this via `realm.create` but had no success either. Any ideas?\r\n\r\n\r\nNote: This question is also [on stackoverflow](http://stackoverflow.com/questions/31772664/realm-update-set-relationship-without-updating-existing-child-object)."},"comment":{"url":"https://api.github.com/repos/realm/realm-cocoa/issues/comments/127048871","html_url":"https://github.com/realm/realm-cocoa/issues/2331#issuecomment-127048871","issue_url":"https://api.github.com/repos/realm/realm-cocoa/issues/2331","id":127048871,"user":{"login":"segiddins","id":1946610,"avatar_url":"https://avatars.githubusercontent.com/u/1946610?v=3","gravatar_id":"","url":"https://api.github.com/users/segiddins","html_url":"https://github.com/segiddins","followers_url":"https://api.github.com/users/segiddins/followers","following_url":"https://api.github.com/users/segiddins/following{/other_user}","gists_url":"https://api.github.com/users/segiddins/gists{/gist_id}","starred_url":"https://api.github.com/users/segiddins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/segiddins/subscriptions","organizations_url":"https://api.github.com/users/segiddins/orgs","repos_url":"https://api.github.com/users/segiddins/repos","events_url":"https://api.github.com/users/segiddins/events{/privacy}","received_events_url":"https://api.github.com/users/segiddins/received_events","type":"User","site_admin":false},"created_at":"2015-08-02T17:42:37Z","updated_at":"2015-08-02T17:42:37Z","body":"@schickling what's happening there is the expected behavior, since your adding the person with a dog with the same id  (the primary key) as the existing one, causing the existing one to be updated. If you don't want to see the existing object updated, you'd have to give the new dog a different primary key value. I hope that helps!"}}