Retrieve the index number of the unique constraint that was most recently violated.
int mco_get_last_unique_violation_index(
/*IN*/ mco_db_h con
);
con
This function returns the internal index number of the unique constraint that was violated during the most recent database operation (e.g., object insertion or update) on the given connection.
The returned value is valid only if the previous operation failed with
MCO_S_DUPLICATE. It can be used to identify which specific unique index caused the violation, especially when a class has multiple unique constraints.Note: The index number corresponds to the order of unique indexes in the class definition (starting from 0). This value is connection-specific and may be overwritten by subsequent operations.
-1 if no unique violation has occurred or if the connection is invalid.
mco_db_h db;
MCO_RET rc;
rc = mco_db_connect("mydb", &db);
if (rc != MCO_S_OK) { /* handle error */ }
/* Attempt to insert a duplicate object */
rc = mco_uda_new(db, class_code, &obj);
if (rc == MCO_S_DUPLICATE) {
int idx = mco_get_last_unique_violation_index(db);
if (idx >= 0) {
printf("Unique constraint violation at index %d\n", idx);
}
}