CoreData SQLite3 Database Constraint Failed Error

Have you ever seen the following error message:

Error Domain=NSCocoaErrorDomain Code=256 UserInfo=0x3baae90 "Operation could not be completed. (Cocoa error 256.)

Talk about not Helpful.

Inside this there was a userInfo dictionary with the following message:

error during SQL execution : constraint failed

Slightly more helpful, but not by very much.

We came across this error message while we were developing our Electrophobe game. We started the debugging process by inserting logging code and pinpointing where the code was failing out. After about 6 hours of work combing through both code and rebuilt databases we finally found the solution.

The trick was that the primary key store for the SQLite3 database that was backing our CoreData NSPersistenceStore had an incorrect value for the Z_MAX column. This column is used, we believe, to house the maximum primary key in order to quickly retrieve it for additional inserts.

As is evident from our escapade however, it is clearly a little vulnerable to tampering when loading the data directly into the SQLite database. To fix the issue, simply update the table with the max(Z_ID) from the referenced table and it should all hook up again.


  • I can\\’t even fathom how long it would have taken me to solve this without your blog post.\\r\\n\\r\\nThank you so much.

  • Guys thanks a lot.\\r\\nYou have just saved me hours of debugging.

  • Same problem here, nice to see someone else is in the same boat as me,but how do you \\”simply update the table with the max(Z_ID) from the referenced table and it should all hook up again.\\”?\\r\\n\\r\\nThanks

  • Use the sqlite3 program from the command line to access the database file and run an update statement to set the maximum ID. You will then have to repackage it for your distribution.

  • That\\’s not he problem, how do yo push the updates to the user\\’s database?

  • Generally this problem happens when you are compiling the initial push of data out to applications. If you had this problem before you pushed your application out then you would not be able to retrieve information from your database.\\r\\n\\r\\nThe CoreData classes manage this just fine if you have not tried to circumvent the CoreData methods of doing this. If you did manage to get this to be broken and you need to push an update into client applications that are already deployed out to end users phones then you are going to have to use the sqlite3 libraries that are on the phone (which are C based) to execute the appropriate SQL queries on the phone as part of an upgrade process. \\r\\n\\r\\nDocumentation for sqlite can be found here:\\r\\nIf you want to look at some info about deploying read-write resources then you can look here:\\r\\n\\r\\nMoving beyond the second link really involves coming up with some sort of versioning token that can be kept around. I suggest using a version table in the database and then building up from there.

  • [...] I got an error exactly same as this post described. [...]

  • [...] searching the web, I found this article that talks about that very message, but it doesn’t seem to apply as apparently the [...]

  • Thanks a lot guys. I was stuck on this issue for some days and project was stuck due to this. But i did what you have told and the issue is gone :)

  • Thanks very much, I was actually getting \\”CoreData: error: (19) constraint failed\\” from a pre-populated database that ships with the application. Turns out I deleted a few items and the Z_PK doesn\\’t recalculate when an item is deleted. \\r\\n\\r\\nThe Z_MAX needs to be the highest value that is actually showing in the Z_PK field NOT the actual number of items, which can be different.\\r\\n\\r\\nYou got me on the right track, thanks very much!

Post a Comment

Your email is not published nor shared.