Establish Relationships using Foriegn Key to Primary Key

268 views
Skip to first unread message

Richard Keys

unread,
Feb 6, 2012, 7:42:45 AM2/6/12
to RestKit
Hi there,

Firstly let me apologise, I'm fairly new to REST & RestKit, so if I
ask any obvious questions please barrage me with abuse appropriately.
Also, I know there's a lot of Stack Overflow questions and posts on
this group - I've read most, if not all of them - about the same
topic. However, I've tried and retried every which way I've read about
to import my objects and haven't really got anything to show for it.

I'm modelling a Rugby Union league system. The Data Model for Core
Data has a League Entity:

League

leagueId
name
createdAt
UpdatedAt

teams - League has many teams

And a Team Entity:

teamId
name
createdAt
UpdatedAt

league - Team has one league

I have the following JSON which represents 1 league "Yorkshire
Division Two":

[{"id":"1","name":"Yorkshire Division
Two","created_at":"1328219134","updated_at":"1328219134"}]

I can map this and import into a Seed DB as follows:

RKManagedObjectMapping* leagueMapping = [RKManagedObjectMapping
mappingForEntityWithName:@"League"];
leagueMapping.primaryKeyAttribute = @"leagueId";
[leagueMapping mapKeyPath:@"id" toAttribute:@"leagueId"];
[leagueMapping mapKeyPath:@"name" toAttribute:@"name"];
[leagueMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[leagueMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];

[objectManager.mappingProvider setMapping:leagueMapping
forKeyPath:@"league"];

RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelInfo);
RKLogConfigureByName("RestKit/CoreData", RKLogLevelTrace);
RKManagedObjectSeeder* seeder = [RKManagedObjectSeeder
objectSeederWithObjectManager:objectManager];

[seeder seedObjectsFromFile:@"leagues.json"
withObjectMapping:leagueMapping];

I can run the app, and the Seed DB gets generated without any
problems. I then have the following in teams.json (though obviously
there'd be more than 1 team per league!):

[{"id":"1","name":"Barnsley","logo":"","league_id":"1","created_at":"1328312288","updated_at":"1328312288"}]

I map this as follows:

RKManagedObjectMapping* teamMapping = [RKManagedObjectMapping
mappingForEntityWithName:@"Team"];
teamMapping.primaryKeyAttribute = @"teamId";
[teamMapping mapKeyPath:@"id" toAttribute:@"teamId"];
[teamMapping mapKeyPath:@"name" toAttribute:@"name"];
[teamMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
[teamMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];

[objectManager.mappingProvider setMapping:teamMapping
forKeyPath:@"team"];
[seeder seedObjectsFromFile:@"teams.json"
withObjectMapping:teamMapping];

Again, teams are added to the data store without issue.

So, is RestKit able to 'Automagically' create the relation from the
league_id foreign key to the correct league object? If so, how? I've
tried several methods of hooking up the relation, all giving me errors
when seeding.

Or, do I have to jump in to the delegate and associate these objects
myself?

Any help would be greatly appreciated :)

Dave Thompson

unread,
Feb 13, 2012, 10:16:29 AM2/13/12
to RestKit
Hi Richard,

Assuming that your leagues are loaded before your teams, then yes -
RestKit can do this for you rather easily. Steps to implement are as
follows. All steps are on the Team entity / JSON / mapping. There
should be no need to change the League entity / JSON / mapping (as you
already have leagueId defined as your primary key there). So:

1) Add a leagueId property to your Team entity (in addition to the
'league' relationship you already have defined there).
2) Ensure that the league_id is included in your Team JSON.
3) Map the JSON league_id onto the leagueId property, in the usual
fashion.
4) Connect the relationship by adding the following code at the end of
your Team mapping:
[teamMapping hasOne:@"league" withMapping:leagueMapping];
[teamMapping connectRelationship:@"league"
withObjectForPrimaryKeyAttribute:@"leagueId"];

That should do it!

I don't tend to monitor the group, so if you post a reply that I need
to read, then drop me a mail directly too so I know to take a look.

Cheers,
Dave
> [{"id":"1","name":"Barnsley","logo":"","league_id":"1","created_at":"132831 2288","updated_at":"1328312288"}]

Vincent Delacourt

unread,
Jun 18, 2012, 12:27:15 PM6/18/12
to res...@googlegroups.com
Dear Dave,

Thank you so much, it's very clear and it works perfectly!

You are the best :)

Josh Benjamin

unread,
Jun 19, 2012, 12:43:00 PM6/19/12
to res...@googlegroups.com
Thank you for clearly and concisely addressing this common point of confusion!

I'm trying to fix the next level of this: many relationships. What then?

Working with the example, what if there was a Member entity and a Member can be on many Teams?

Frederic Visticot

unread,
Jun 19, 2012, 3:38:56 PM6/19/12
to res...@googlegroups.com
Sorry to not answer your question but I'm very interested in the answer...
I need to solve the same pb you have...
If I can find the solution, I will publish the answer on this forum...

Fred
Reply all
Reply to author
Forward
0 new messages