title: IOS - How to Insert data into different tables with relationship In Core Data Swift
body: <p>We have two tables with one to one relationship. <code>personel.idpersonel</code> maps to <code>personeltochart.idpersonel</code>. </p>
  
  <ol>
  <li>personel</li>
  <li>personeltochart</li>
  </ol>
  
  <p>On login event, we sync data of <code>personel</code> table from a webservice, and save it into <code>personel</code> tables. We have to get other table data on another event and save it.</p>
  
  <p>The question is when we try to fetch data from these two tables why we get data of <code>personel</code> table correctly but data from <code>personeltochart</code> table is nil?</p>
  
  <h2>personel</h2>
  
  <p>firstname  <br>
  idpersonel</p>
  
  <h2>personelToChart</h2>
  
  <p>id  <br>
  idpersonel</p>
  
  <p>Here is an example of saving data of table:</p>
  
  <pre><code>                    let jsonEmza = try jsonDecoder.decode([JSONNameTabel].self, from: data)
  
                      var privateContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
                      privateContext = CoreDataManager.shared.persistentContainer.viewContext
  
  
                      jsonEmza.forEach({ (jsonCompany) in
                          let tabel = jsonCompany.tableName
                          if tabel == "Personel"{
  
                          jsonCompany.personel?.forEach({ (field) in
                                  let personel = Personel(context: privateContext)
                                  personel.firstname = field.firstName
                                  personel.idpersonel = field.id
  
                              })
   do {
  
                              try privateContext.save()
  
                          } catch let saveErr {
                              print("Failed to save personel:", saveErr)
                          }
  
                          }
  </code></pre>
  
  <p>fetching example</p>
  
  <pre><code>    let context = CoreDataManager.shared.persistentContainer.viewContext
  
      let fetchRequest = NSFetchRequest&lt;Personel&gt;(entityName: "Personel")
     let predicate = NSPredicate(format: "idpersonel == %@", "147")
     fetchRequest.predicate = predicate
      do {
          let listNameh = try context.fetch(fetchRequest)
          listNameh.forEach({ (item) in
              print(item.idpersonel ?? "")
              print(item.lastname ?? "")
              print(item.firstname ?? "")
              print(item.personelToChart?.id)
  
          })
  
  
      } catch let fetchErr {
          print("Failed to fetch Personel:", fetchErr)
  
      }
  </code></pre>
  
  <p>The data of <code>item.personelToChart?.id</code> is <code>nil</code>. </p>
  
  <p>Why is this happening?</p>