Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8005759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:18:38+00:00 2026-06-04T17:18:38+00:00

I have a tough issue that I cannot find an answer to. My data

  • 0

I have a tough issue that I cannot find an answer to. My data model is structured like this:

Version 1:
project has many locations
location has many projects
But by mistake the inverse between the two was never set.

Version 2:
The same as above, but the inverse is now setup.

An example of my problem would be as follows:
In version 1, I have two projects that own the same location. When I launch version 2 and my mapping model is processed, the original project to own the location loses it’s relationship to that location and now the location only shows up as part of one of the projects instead of both of them.

I recognize that this issue is likely caused by me not setting up the inverse relationship between projects and locations, but is there anything that I can do to make data persist across the two versions of the app/data model?

Edit:
I have tried an inferred mapping model and I have tried creating a mapping model manually. I am currently only using the NSMigratePersistentStoresAutomaticallyOption key when I created my NSPersistentStoreCoordinator.

Also, just to be clear, I have two versions of my data model and the migration is successfully occurring, the only problem is that the relationships are not persisting as intended.

Edit 2:
I have figured out that I will need to subclass NSEntityMigrationPolicy. I do not want to do an entirely custom migration, I would prefer to keep the rest of my migration automatic if possible. Does anyone know of any good tutorials or examples on subclassing NSEntityMigrationPolicy that would be relevant to my purpose? I haven’t been able to find much and as far as I can tell there is extremely little reference to it in Apples docs.

Edit 3:
I cannot for the life of me figure out how to setup an inverse relationship using NSEntityMigrationPolicy. My problem is a bit different now than I described earlier. Does anyone know of any solid example on how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:18:39+00:00Added an answer on June 4, 2026 at 5:18 pm

    For anyone else that comes across this situation, there was actually a very simple answer that I had not considered.

    I did not get too far with attempting to subclass NSEntityMigrationPolicy — I couldn’t find any good sample code that went beyond the basics. In certain situations, subclassing NSEntityMigrationPolicy may be the best solution. But if you are in the same situation as me — (meaning you have setup the correct fields for inverse relationships in your model but you forgot to actually specify the inverses), I believe I have a simpler solution.

    Instead of subclassing NSEntityMigrationPolicy, I did the following (which is actually much simpler IMO):

    (keep in mind that my model is setup as follows: Projects <–> Locations, a has-and-belongs-to-many relationship)

    1. I kept my old data model (.xcdatamodel) file in my app. When my app first loads, it loads my NSManagedObjectModel from the old data model file. I then proceeded to loop through all of the projects in my database, and through all of the locations for each project, and for each location I would set the “project” field manually — if I had set up my model correct the first time, this would have been completed automatically using inverse relationships. Here is what my code looks like:

      NSArray *projects = [context executeFetchRequest:request error:&error];
      for(Project *project in projects) {
          for(Location *location in project.locations) {
              // set the inverse relationship manually
              [location addProjectsObject:project];
          }
      }
      
    2. I saved my NSManagedObjectContext.

    3. I then got rid of my NSManagedObjectContext, NSManagedObjectModel, NSPersistentStoreCoordinator and rebuilt them using my new xcdatamodel file. The new model file contains the inverse relationships and they are setup correctly. When migrating the data from the SQLite database, all of the data persisted and the inverse relationships that I setup manually were still in place. When creating the new NSPersistentStoreCoordinator, make sure to specify the NSMigratePersistentStoresAutomaticallyOption option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a listing of data that has a randomly generated order listing.
I have tough time making this design decision. I could go with traditional new
I have a tough problem. I want to make a .dll that is netload-ed
I have a strange issue. It almost seems like a bug. if ($object->error) {Form::BuildReturn();
I have a class that contains a static number of objects. This class needs
I have a table that has a composite key that consists of two int
I've been searching for an answer to this but I can't seem to find
Hopefully, someone here can give me some light. I have been researching this issue
This is a tough question to word...so bear with me. I have two tables
I have a very strange issue that only affects webkit browsers for some reason,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.