Imaginea/sugarcrm-android — Issue #5: SQLiteConstraintException while trying to insert relationship info
Author: vasavikumar
Created: 2011-12-28 14:10:20+00:00
State: open

Opportunities can be related to contacts and contacts can be related to opportunities.

While syncing, we are retrieving the related opportunities for a contact and while retrieving opportunities, we are retrieving the related contacts as well. So, while trying to insert the relationship, the insertion fails if the relationship already exists in the db.

The log for the same is as follows:
E/Database( 6211): Error inserting opportunity_id=50 deleted=0 contact_id=3
E/Database( 6211): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
E/Database( 6211):	at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
E/Database( 6211):	at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
E/Database( 6211):	at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
E/Database( 6211):	at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
E/Database( 6211):	at com.imaginea.android.sugarcrm.provider.SugarCRMProvider.update(SugarCRMProvider.java:1562)
E/Database( 6211):	at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:225)
E/Database( 6211):	at android.content.ContentProvider.applyBatch(ContentProvider.java:824)
E/Database( 6211):	at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
E/Database( 6211):	at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
E/Database( 6211):	at android.content.ContentResolver.applyBatch(ContentResolver.java:639)
E/Database( 6211):	at com.imaginea.android.sugarcrm.sync.BatchOperation.execute(BatchOperation.java:75)
E/Database( 6211):	at com.imaginea.android.sugarcrm.sync.SugarSyncManager.syncModulesData(SugarSyncManager.java:193)
E/Database( 6211):	at com.imaginea.android.sugarcrm.sync.SyncAdapter.syncModuleData(SyncAdapter.java:222)
E/Database( 6211):	at com.imaginea.android.sugarcrm.sync.SyncAdapter.syncAllModulesData(SyncAdapter.java:193)
E/Database( 6211):	at com.imaginea.android.sugarcrm.sync.SyncAdapter.onPerformSync(SyncAdapter.java:136)
E/Database( 6211):	at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:164)

We'll have to update the code to not to fetch the relationships twice (once while fetching contacts and once while fetching opportunities).