How to Add Unique Constraints for Some Fields in Core Data

core data ios9: multifield unique constraint

It is possible to set uniqueness constraints for a combination of attributes. You were on the right path putting both attributes on the same line in the constraints:

Data Model Editor snapshot

You may have found this had no effect due to a problem with Xcode: the constraints are not actually updated in the model unless you modify some other aspect of the model at the same time (eg. change an attribute type and then change it back).

If you look at the SQL being generated, the table is created with a constraint:

CREATE TABLE ZSTOREOBJECT ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ...., CONSTRAINT ZLASTNAME_ZFIRSTNAME UNIQUE (ZLASTNAME, ZFIRSTNAME))

and when you insert new insert new values which fail this constraint (when the context is saved):

CoreData: sql: COMMIT
CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: INSERT INTO ZSTOREOBJECT(Z_PK, Z_ENT, Z_OPT) VALUES(?, ?, ?)
CoreData: sql: UPDATE ZSTOREOBJECT SET ZLASTNAME = ?, ZFIRSTNAME = ? WHERE Z_PK = ?
CoreData: sql: ROLLBACK

and the error returned is:

Error Domain=NSCocoaErrorDomain Code=133021 "(null)" UserInfo={conflictList=(
"NSConstraintConflict (0x7fbd18d33c10) for constraint (\n lastName,\n firstName\n): ....

(This works in Xcode 7.2.1, with the iOS9.2 simulator; I haven't checked prior versions).

Core Data Unique constraint except for the default value

Core Data's uniqueness constraints are just that-- they require uniqueness, without exceptions. Your situation of wanting "unique except for one value that can be duplicated" isn't directly supported by Core Data. You'd have to maintain that in your own code somehow. That probably means implementing your own update-or-insert logic to check whether an ID exists. That would be something like,

  1. Do a fetch with the specific ID.
  2. If you find an object update it.
  3. If you don't find an object, create a new one.

Core Data unique attributes

I've decided to use the validate:error: method to check if there is already a Managed Object with the specific value of . An error is raised if this is the case.

For example:

- (BOOL)validateMyAttribute:(id *)value error:(NSError **)error {
// Return NO if there is already an object with a myAtribute of value
}

Thanks to Martin Cote for his input.

CoreData unique constraints: how to get field name on which constraint checking fails

AFAIK Nor NSConstraintConflict neither something like NSConstraint is documented. However, the constraint conflict knows the constraint:

NSConstraintConflict (0x7ff12b84f600) for constraint ( identity ): database: (entity: Person; id: 0xd000000000040000 ; data: ), conflictedObjects: ( " (entity: Person; id: 0xd000000000140000 ; data: {\n identity = 1111;\n name = Name1;\n})" )

I would bet that you can get the constraint with

[conflict valueForKey:@"constraint"]

Look for the class of the result.

Unique Constraints do not work

Unique Constraints only work when you save the context.

After creating NSMangaedObjects save that NSManagedContext in which they were created.

Yes unique constraints doesnot work if you have relationship in your entity.(I tried this with xcode 7.3 ios9)

Core Data Entity Unique Constraint Does Not Work

If you want to get an error when there are merge conflicts and handle them manually then you need to change your policy to NSErrorMergePolicy and you will get an error and in the user info the object IDs that you need to solve merge conflict , otherwise it will merge and save according to the specified merge policy.
The policy that you set will overwrite the object attributes but not relationships, if you want to overwrite the attributes and relationships then specify the NSMergeByPropertyObjectTrumpMergePolicy.



Related Topics



Leave a reply



Submit