I am a new APEX learner. I created two custom objects : Book & Author, where Author being Parent and Book being child object. I created Child : book record 'Harry Potter' with Parent : 'J K Rowling'.
Now I Wanted to Change the author to myself so I performed the following operation:
Author__c aut=[SELECT Id FROM Author__c WHERE Name LIKE 'Ravi%'];
system.debug('Author '+aut.Id);
Book__c bc=[SELECT Id, Name, Author__r.Id, Author__r.Name FROM Book__c WHERE Name LIKE 'harry potter%'];
system.debug(bc.Name+', Author: '+bc.Author__r.Name+', author Id: '+bc.Author__r.Id);
bc.Author__r.Id=aut.Id;
system.debug(bc.Author__r.Id);
update bc;
bc=[SELECT Id, Name, Author__r.Id, Author__r.Name FROM Book__c WHERE Name LIKE 'harry potter%'];
system.debug(bc.Name+', Author: '+bc.Author__r.Name+', author Id: '+bc.Author__r.Id);
This Executed successfully, but author wasn't changed :