I have been using Core Data to model my database. I have 2 entities that are in many to many relationship. Each person can have many addresses, and on each address multiple persons can live.
Now i would like to add property to this relationship. For example one person - address will have label home, other person - address can will have label mama's place.
I can't add this property on address entity, because same address will have different labels for different persons.
Since relationships are modeled like NSSets, I don't see a way to do what I want.
Can this be somehow done?
It is not possible to add attributes to a many-many relationship directly. The Apple-recommended approach (see "Modelling a relationship based on its semantics" in the CoreData Programming Guide) is to replace the many-many relationship with an intermediate entity, to which you add the attributes. Each of your existing entities will have a one-many relationship with the new entity.
In your case, you might have something like this:
Person <--->> PersonAddressDetails <<---> Address
You can then add the label attribute to the PersonAddressDetails entity.
Related
I have a problem how to model properly in CoreData a Person with it's relationship to children with inverse using single entity to model.
I want to model a family, so I have created an entity Person for each member. A Person can have parents, a father and mother - for this I have created one-to-one relationship. Person can have children which is a set of one-to-many relationship.
I Have a problem with setting up the inverse in CoreData model as it is required for the relationships. The problem is that when I set father/mother attribute of entity Person to children it can only be set for only one of them, not for both a father and mother. Do you have a solution how to model this properly?
There's multiple ways you can go about this.
Personally, I would just have a parents and a children property and leave it at that.
If you really want to differentiate between mother and father, you could use a single parents/children relationship and look for the Person with the required gender.
Another alternative is to create a ParentRelationship object that points to a parent and a child and describes the relationship.
In Core Data I have a House Entity which has a relationship to a Room entity. The Room entity itself has a many-to-many relationship with a Door entity.
Two rooms might share the same door, so here is what I need to know:
Is it possible to have two Room entities with a relationship to the same Door entity. And when updating the properties of the Door entity, it will be the same object and gets updated in both Room entities.
Is it possible to move the Door entity from one relationship to another, without copying it?
You have a many-to-many relationship between Room and Door. This means that those relationships behave as a Set, so you can call myDoor.doors.remove(room), or .insert() to manage the entity. These are reciprocal as well, like all Core Data relationships (see here for further details). Do note your naming scheme is a bit confusing, and for example I'd recommend switching to using myDoor.rooms
I would like to create a model with two entities Orders and Products. They are linked by a relationship 'Contain' that has an attribute 'quantity'. How can I represent that in CoreData ? (Do not send me the ray tutorial, or any tutorial on youtube, I think I have done every thing). It is very important the relationship with attribute and not something general. (I know that it is not a database, but it is a Conceptual data model/Conceptual Schema as it is named by "entities" and "relationship" so if there is relationships, there must be a way to have relationships with attribute).
EDIT :
Am I doing the right thing by not adding id_order and id_product to the Contain entity ?
In CoreData, Contain would be another entity. It would have relationships to Orders and Products and a quantity attribute.
You cannot add attributes to a relationship in CoreData.
I have created a database in which I have to store contacts in various categories. The issue comes when I have to create Sub categories in a Category like :-
Categories ->
Sub Categories->
Contacts
But the Categories can also have Contacts like
Categories -> Contacts
where the sub categories can also have contacts. I figured that nesting in core data would be used. How can I achieve this kind of a relationship ? How do I save the sub categories into the categories even though they are of the same entity ?
I have attached my core data entity relationship model here :-
There is no problem creating a "self" referencing relationship in CoreDate.
In other words, An entity may have a relationship of its own kind.
The only difference in your case between a Category and a SubCategory is the existence of a parent entity.
So there is no need to define a new entity for that part.
You can simply define a relationship:
Category.parent of type Category (say to-one in this case)
and a reverse relationship of:
Category.subCategories of type NSSet (to-many in this case) containing Category objects.
You can set all that up in your interface builder.
Now, since Category has a relationship with Contact so does all the "sub-categories" will have that relationship.
If you like your "sub-categories" to have additional properties, simply create a new entity an make it inherit from your Category entity (keeping the above setting I described).
and add to it the new properties.
I have two entities: patient and checkpoint.
Patient has attributes such as DOB, name, ID, etc.
Checkpoint has attributes such as dateRecorded, height, weight, etc.
You probably get the idea- I want there to be a set of patients, and then each patient can have checkpoints associated with that patient.
On both entities, how should I set the settings? The settings are:
I looked at the documentation for this, and I was still confused. I think what I want is a one to many relationship (for patient), but then I'm not sure how to set the inverses for either of them, or the delete rule and the other stuff. THANK YOU!!
I just got started with Core Data this week. Great question!
Relationships:
Since one patient can have many checkpoints, the Patient to Checkpoint relationship is a One to Many relationship. The concept of an "inverse relationship" is essentially this: You've got a relationship going one way (Patient to Checkpoint) - now go ahead and look at it from the inverse, the Checkpoint's perspective. A checkpoint can apply to only a single patient. Therefore, the Checkpoint to Patient relationship is a One to One relationship.
Inverse Relationships:
To handle the inverse relationship, simply create each relationship, ignoring the inverse. Then, after you have the relationship on each object, go ahead and define the inverse as the relationship on the other entity.
In other words, a relationship points to another entity or a group of entities. An inverse relationship points to a relationship on another entity.
Delete Rules:
As far as delete rules are concerned, it's fairly simple. When trying to delete a patient which has checkpoints...
Deny: Core Data won't let you delete the Patient.
Cascade: Core Data will delete the Entity (Patient), as well as cascading through relationships and deleting those objects as well. (In other words, Core Data will delete the Checkpoint objects too.)
Nullify: Core Data will delete the patient but first remove the relationship. The Checkpoint will remain intact.
For the Patient entity might want either deny or cascade, depending on how you want to manage your data. Based on your usage case, you probably don't want nullify, since Checkpoints are dependent upon Patient entities.
You want nullify for the Checkpoint, since the Cascade would prevent you from deleting a checkpoint without deleting the entire patient, and Deny would effectively force the same.
Based on the scenario mentinoed, it looks like a one to many relationship between patient and checkpoint tables.
Now add a relationship from “Patient” to “Checkpoint”, and also set the inverse between the tables.
Also, set the delete rule for both relationships to “cascade”. This means that if you delete one object with Patient, the corressponding Coredata will delete the associated object as well.