Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData: uniqueness constraints and to-one mandatory inverse relationship

Tags:

core-data

Please, explain me, why core data denies to create structure shown below.

    |Employee |                                            |Department|
    |------   | <<--(opt)-------inverse-------(non_opt)--> |----------|
    |id (un)  |                                            |name (un) |
    |fullName |

    * un - unique, opt - optional

At some department zero or more employees can work but every employee must works at one and only at one department. It is obvious schema for MySql, for example. For Core Data I made inverse relationship for avoiding consistency errors. But Core Data has its own vision and this is what it says for me:

error: Misconfigured Entity: Entity Department cannot have uniqueness constraints and to-one mandatory inverse relationship Employee.department

I have already found some suspicious solutions (make all relations optional, remove unique from Department.name and check it "programmatically", remove inverse relations), but I want to understand, where I make mistake or if not - what Apple is trying to said with this strange logic?

like image 872
Anton Avatar asked May 31 '16 12:05

Anton


People also ask

What is inverse relationship Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.

How do you make a Core Data attribute unique with constraints?

If you look in the Data Model inspector you'll see a field marked "Constraints" – click the + button at the bottom of that field. A new row will appear saying "comma,separated,properties". Click on that, hit Enter to make it editable, then type "sha" and hit Enter again. Make sure you press Cmd+S to save your changes!

What is constraints in Core Data?

Constraints in Core Data are part of an entity configuration. Settings like the entity name and Spotlight display name might be easy to understand while constraints are a bit less known. However, they can be super useful to maintain a unique set of data.

What is entity in Core Data in Swift?

An entity describes an object, including its name, attributes, and relationships. Create an entity for each of your app's objects.


1 Answers

Unique Constraints make sure that records in an Entity are unique by the given fields. But unique constraints along with To-Many relationship leads to a lot of weird issues while resolving conflicts.

e.g. “Dangling reference to an invalid object.”

This post is basically focused to a small problem that may take days to fix.

http://muhammadzahidimran.com/2016/12/08/coredata-unique-constraints-and-to-many-relationship/

like image 126
Zahid Avatar answered Oct 11 '22 07:10

Zahid