Core Data SQLite Many-to-many relationships

Discussion in 'iOS Programming' started by krayziekray, Sep 1, 2011.

  1. krayziekray macrumors 6502

    Joined:
    Sep 24, 2008
    Location:
    127.0.0.1
    #1
    Hi,

    In my iOS app I'm loading an sqlite file from the app bundle into core data. This works perfectly fine, however, I want to know how i can add a many-to-many relationship in an SQL statement so i can populate my Database with the correct relationships. Let me explain a bit further.

    I have two Entity's in my Core Data model. House and Owner. These two classes have a many-to-many relationship, so a House can have many Owners and a Owner can have many Houses.

    I have set up the data model with the appropriate relationships, however, I'm not sure how to create the link in an SQL Statement.

    With a one to many relationship I would simply add the Z_PK of the House row to the Z_OWNER column which would mean the house belongs to this owner. But how can I add a group of owners? something along the lines of {x, y, z} in the Z_OWNER column which would state the the Z_PK x,y&z (from the Houses entity) are the owners of this house and vice versa?

    I hope you can help me :)

    Thanks in advance! :)

    KrayzieKray
     
  2. Hansr macrumors 6502a

    Joined:
    Apr 1, 2007
    #2
    If i understand correctly you should have a separate ownership table, e.g. Owners, Houses and then Ownership tables and the Ownership table has House -> Owner links.
     
  3. krayziekray thread starter macrumors 6502

    Joined:
    Sep 24, 2008
    Location:
    127.0.0.1
    #3
    Yeh, that's how I would develop my database if i were using a normal DBMS, but I'm just confused as to why there are many to many relationships in the Model in Xcode. Does core data create the third table? How do I get it to create this third table?

    Sorry, I'm new to Core Data :eek:
     
  4. hayesk macrumors 65816

    Joined:
    May 20, 2003
    #4
    Yes, it creates an internal table. FYI, I've found it easier just to forgo CoreData and use sqllite directly.
     

Share This Page