@@ -1588,7 +1588,7 @@ JNIEXPORT jlong JNICALL Java_io_realm_internal_Table_nativeSetPrimaryKey(
15881588 try {
15891589 Table* table = TBL (nativeTablePtr);
15901590 Table* pk_table = TBL (nativePrivateKeyTablePtr);
1591- const char * table_name = table->get_name ().data ();
1591+ const char * table_name = table->get_name ().substr ( 6 ). data (); // Remove "class_" prefix
15921592 size_t row_index = pk_table->find_first_string (io_realm_internal_Table_PRIMARY_KEY_CLASS_COLUMN_INDEX, table_name);
15931593
15941594 if (columnName == NULL || env->GetStringLength (columnName) == 0 ) {
@@ -1627,35 +1627,57 @@ JNIEXPORT jlong JNICALL Java_io_realm_internal_Table_nativeSetPrimaryKey(
16271627 return 0 ;
16281628}
16291629
1630- // Fixes interop issue with Cocoa Realm where the Primary Key table had different types.
1630+ // 1) Fixes interop issue with Cocoa Realm where the Primary Key table had different types.
16311631// This affects:
16321632// - All Realms created by Cocoa and used by Realm-android up to 0.80.1
16331633// - All Realms created by Realm-Android 0.80.1 and below
16341634// See https://github.com/realm/realm-java/issues/1059
1635- // This methods converts the old (wrong) table format (string, integer) to the right (string,string) format.
1635+ //
1636+ // 2) Fix interop issue with Cocoa Realm where primary key tables on Cocoa doesn't have the "class_" prefix.
1637+ // This affects:
1638+ // - All Realms created by Cocoa and used by Realm-android up to 0.84.1
1639+ // - All Realms created by Realm-Android 0.84.1 and below
1640+ // See https://github.com/realm/realm-java/issues/1703
1641+
1642+ // This methods converts the old (wrong) table format (string, integer) to the right (string,string) format and strips
1643+ // any class names in the col[0] of their "class_" prefix
16361644JNIEXPORT void JNICALL Java_io_realm_internal_Table_nativeMigratePrimaryKeyTableIfNeeded
16371645 (JNIEnv*, jobject, jlong groupNativePtr, jlong privateKeyTableNativePtr)
16381646{
1647+ const size_t CLASS_COLUMN_INDEX = io_realm_internal_Table_PRIMARY_KEY_CLASS_COLUMN_INDEX;
1648+ const size_t FIELD_COLUMN_INDEX = io_realm_internal_Table_PRIMARY_KEY_FIELD_COLUMN_INDEX;
1649+
16391650 Group* group = G (groupNativePtr);
16401651 Table* pk_table = TBL (privateKeyTableNativePtr);
1641- if (pk_table->get_column_type (io_realm_internal_Table_PRIMARY_KEY_FIELD_COLUMN_INDEX) == type_Int) {
1652+
1653+ // Fix wrong types (string, int) -> (string, string)
1654+ if (pk_table->get_column_type (FIELD_COLUMN_INDEX) == type_Int) {
16421655 StringData tmp_col_name = StringData (" tmp_field_name" );
16431656 size_t tmp_col_ndx = pk_table->add_column (DataType (type_String), tmp_col_name);
16441657
16451658 // Create tmp string column with field name instead of column index
16461659 size_t number_of_rows = pk_table->size ();
16471660 for (size_t row_ndx = 0 ; row_ndx < number_of_rows; row_ndx++) {
1648- StringData table_name = pk_table->get_string (io_realm_internal_Table_PRIMARY_KEY_CLASS_COLUMN_INDEX , row_ndx);
1649- size_t col_ndx = static_cast <size_t >(pk_table->get_int (io_realm_internal_Table_PRIMARY_KEY_FIELD_COLUMN_INDEX , row_ndx));
1661+ StringData table_name = pk_table->get_string (CLASS_COLUMN_INDEX , row_ndx);
1662+ size_t col_ndx = static_cast <size_t >(pk_table->get_int (FIELD_COLUMN_INDEX , row_ndx));
16501663 StringData col_name = group->get_table (table_name)->get_column_name (col_ndx);
16511664 pk_table->set_string (tmp_col_ndx, row_ndx, col_name);
16521665 }
16531666
16541667 // Delete old int column, and rename tmp column to same name
16551668 // The column index for the renamed column will then be the same as the deleted old column
1656- pk_table->remove_column (io_realm_internal_Table_PRIMARY_KEY_FIELD_COLUMN_INDEX );
1669+ pk_table->remove_column (FIELD_COLUMN_INDEX );
16571670 pk_table->rename_column (pk_table->get_column_index (tmp_col_name), StringData (" pk_property" ));
16581671 }
1672+
1673+ // If needed remove "class_" prefix from class names
1674+ size_t number_of_rows = pk_table->size ();
1675+ for (size_t row_ndx = 0 ; row_ndx < number_of_rows; row_ndx++) {
1676+ StringData table_name = pk_table->get_string (CLASS_COLUMN_INDEX, row_ndx);
1677+ if (table_name.begins_with (" class_" )) {
1678+ pk_table->set_string (CLASS_COLUMN_INDEX, row_ndx, table_name.substr (6 ));
1679+ }
1680+ }
16591681}
16601682
16611683JNIEXPORT jboolean JNICALL Java_io_realm_internal_Table_nativeHasSameSchema
0 commit comments