Primary and secondary constraints in core data - ios

Is there such a thing as primary and secondary constraints in core data?
I have a one to many relationship. In the "many" entity, I would like to have a constraint for one attribute. The problem is that the constraint is for all entities that I create and not for the specific one that is related to the "one" entity in the relationship.

Related

Core Data relationship between existing entities

I have two tables of data. One is table_A(id, x, x, b_id) and table_B(id, x).
I would like to add a relationship between b_id from table_A to id of table_B. I already have a JSON data like that, and I tried with Xcode to make so connection, but all I can make is a new relationship between those two.
I'm new to this, so would apreciate any help.
You are thinking of CoreData in terms of a DBMS which it is not. You don't need to set up foreign keys to make relationships in CoreData. If you want to assign a B table entity to a A you just create a relationship of between the two and you can set the attribute. The foreignKey and linking is all done by CoreData in the background.

Why entity cannot have uniqueness constraints with to-one mandatory inverse relationship?

Why entity cannot have uniqueness constraints with to-one mandatory inverse relationship?
Having two entities:
Person
property: name
relationship: department (to-one, non-optional)
Department
property: title (unique constraint)
relationship: person (to-many, optional)
Model won't compile in iOS 9, XCode 7.0.1 with misconfigured entity error:
Misconfigured Entity: Entity Department cannot have uniqueness
constraints and to-one mandatory inverse relationship
Person.department
Update:
Question is still relevant in XCode 8.3.1.
Short answer:
The underlying problem is most likely caused by the sqlite standard. I'm not sure about that. How ever, it's very likely that it is because of the limitations of sqlite. I found some posts on the internet, where people had issues with multiple constraints on one table and thats most likely the reason why the two-table workaround works.
Long answer:
It's pretty late, but I hope it helps anyway.
This occurs when your Entity has an unique constraint and a mandatory relation. I guess it's because of the added unique constraint behaviors in iOS 9.0. However you can solve this in two ways:
You remove the unique constraint or make the relation optional. You could handle an optional relation in code. But it won't be a nice solution.
OR
You can use a workaround. You can have both. You can create a super class having a unique constraint. However this won't work without problems, too.
Let's you have three entities. A, B and C.
A is your super class and B is a sub class of A and C is a sub class of A, too. A has an unique constraint on it's property primaryKey. When saving instances of B and C, you can not have a B and C with the same primaryKey. Because CoreData will manage both as A.
You could change A to have two Properties:
int: originalPrimaryKey (NO unique constraint)
string: primaryKey (unique constraint)
You can now map your primaryKeys to originalPrimaryKey and when setting the originalPrimaryKey you could set the string primaryKey property to CLASS_NAME.{originalPrimaryKey}. This would allow you to have the behavior, you would expect. But you have to add a workaround for primaryKeys.
If you are trying to add constraints with existing entity with records ,
The solution is
1. delete all records from existing entity
2. delete existing relationships with the entity .
3. Regenerate the +CoreDataClass and +CoreDataProperties agin.
Make your relationship properties "optional". That fixed this problem in my case.

Why can't order id be placed as a foreign key in the customer table?

"The unique identifier of the entity on the “one side” of the one-to-many relationship is placed as a foreign key in the table representing the entity on the “many side.”
Why can't the primary key of the entity on the "many side" of the one-to-many relationship be placed as a foreign key in the table representing the entity on the "one side" ?
Ex. one-to-many relationship between Customer and Order
A customer can place many orders, but each order can be placed by only one customer.
Why can't order id be placed as a foreign key in the customer table?
Any one entity can have only one value of the foreign key, and therefore can reference only one related entity. The referenced entity is thus on the (a) "one" side of the relationship by definition. The entity bearing the FK can be either the other "one" side of a one:one relationship or the "many" side of a one:many relationship; the difference is whether foreign key values may be duplicated in more than one entity of that type.

core data - relationships, fetching and inserting

I'm using CoreData for the first time in one of my project. My table involves FOREIGN KEYS and since CoreData doesn't support FOREIGN KEYS, I'm having some issues.
Below is the structure of my tables.
My Problem is the establishment attribute.The establishment attribute is supposed to hold the name of a particular facility from the Facilities table. However, since it's a relationship, Xcode expects a Facility rather than just a name of a facility (NSString).
Is this possible, or am I just mixing up FOREIGN KEYS with RELATIONSHIPS in CoreData? How would I solve this problem?
Thanks in advance.
A relationship is not a property, so it does not have a type. In the model editor you add a relationship explicitly.
From your diagram, I see that you did not set the inverse relationship. There needs to be a corresponding relationship from name to the Assessors entity. (Set the "Destination" to Assessors in the model editor.)
I would also suggest to rename a few items.
First, use singular: Assessor, Facility. These are objects (comparable to classes), not tables.
Second, because your name attribute refers to an assessor, call it assessor, and similarly call its reverse relationship facilities (it is a to-many relationship in this direction, so the plural is appropriate).
If you need the name of the assessor of a facility, you use
facility.assessor.name
This should make it obvious why you do not need foreign keys. Indeed, I would urge you to think that it is not Core Data that does not support foreign keys, but that it is the traditional relational databases that do not support relationships!

Entity Framework 4: Can't add Referential Constraint to Assiciation with Model First and GUID Ids

I read a lot of examples and tutorials about adding referential constraints but my designer just doesn't give me the FK I would need to select.
I'm using model first and all my IDs are GUIDs.
These are the two entities:
These are the properties of their association:
And this is the "Referential Constraint Dialog":
As you can see: There is no FK to select as "Dependent Property"...
In the database there is a FK column for that:
What am I doing wrong?
Thanx
Because your entity doesn't have any FK property. You must first create the property which will be used as your FK and select it in Dependent Property drop down (it shows only existing properties of dependent entity).

Resources