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?
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.
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!
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.
An entity describes an object, including its name, attributes, and relationships. Create an entity for each of your app's objects.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With