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 3230454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:55:44+00:00 2026-05-17T16:55:44+00:00

Above is my data model diagram. Everytime i create a Car whcih contains a

  • 0

alt text

Above is my data model diagram.
Everytime i create a Car whcih contains a model and make, it adds a car object to core data.
The problem is that it also adds 1 make and 1 model to the core data, so i get duplicates.

for example, in the following code, it saves 1 car object 2 models, and 2 makes,
so I get a duplicate Make in my table (2 “Nissan”).
How can i avoid this? is it possible to create primary keys?

In the following example i want to assume that make and models already exists, and a car is only referencing to them, so how can I avoid inserting into make, and model, and only insert into car?

- (void)MyCode
{
     [self AddCar:@"Nissan" @"Rogue"];
     [self AddCar:@"Nissan" @"Murano"];
}


- (void)AddCar :(NSString*)_make :(NSString*)_model
{
    Car *car = [[Car alloc] initWithEntity:[NSEntityDescription entityForName:@"Car" 
                                                       inManagedObjectContext:self.managedObjectContext]
            insertIntoManagedObjectContext:self.managedObjectContext];
    Make *make = [[Make alloc] initWithEntity:[NSEntityDescription entityForName:@"Make" 
                                                       inManagedObjectContext:self.managedObjectContext]
            insertIntoManagedObjectContext:self.managedObjectContext];
    make.name = _make;

    Model *model = [[Model alloc] initWithEntity:[NSEntityDescription entityForName:@"Model" 
                                                          inManagedObjectContext:self.managedObjectContext]
               insertIntoManagedObjectContext:self.managedObjectContext];
    model.name = _model;
    car.make = make;
    car.model = model;
    [self saveContext];
}
  • 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-05-17T16:55:45+00:00Added an answer on May 17, 2026 at 4:55 pm

    There are a few issues here.

    The data model shows that the Make and Model entities each have a to-one relationship with Car. Therefore, there can be only one Car for each Make and only one Car for each Model. In other words, given that data model, Nissan can only make one Car. You probably want a to-many cars relationship in Model so that Nissan can make more than one Car. Same thing with Make so that there can be more than one Car with a given Make.

    The way NSManagedObjects are inserted into the managed object context is incorrect. It should be done like this:

    Car *car = [NSEntityDescription
        insertNewObjectForEntityForName:@"Car"
        inManagedObjectContext:self.managedObjectContext];
    

    Finally, your code shows that a new Make and Model entity are created for each Car. If you are starting with the name of a Make, you probably want to search the managed object context for a Make that has a matching name. If found, just set the make relationship of the new Car to the Make entity that is already in the context. If not found, create a new Make entity and set up a relationship to that.

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

Sidebar

Related Questions

I have one data model 'object' with fields->object_id, object_name. That is: http://localhost:3000/objects/ I have
I'm trying to create a generic data model that will allow for a particular
From the above sample data, i want to write a query that will return
<input name='data[User][images][]' id='images' type='file' multiple= /> I know that the above works and allows
My data model contains two tables with composite primary keys and an associative table.
I have a one-to-many relationship in my core data model. There can be many
How do I implement cross-model fetched properties in a core data model? Simply put,
I have 2 advanced data grids, one above the other. They both have the
alt text http://img16.imageshack.us/img16/8085/datacontext.jpg Above is the LINQ to SQL designer view for my data
I'm using NSURLConnection with sendAsynchronousRequest method (and handing the data in block). Using above

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.