diff --git a/src/realm/object-store/c_api/notifications.cpp b/src/realm/object-store/c_api/notifications.cpp index 236a102b375..dd087acacb2 100644 --- a/src/realm/object-store/c_api/notifications.cpp +++ b/src/realm/object-store/c_api/notifications.cpp @@ -73,7 +73,7 @@ std::optional build_key_path_array(realm_key_path_array_t* key_pat return ret; } -static KeyPathArray create_key_path_array(const ObjectSchema& object_schema, const Schema& schema, +static KeyPathArray create_key_path_array(const Group& group, const ObjectSchema& object_schema, const Schema& schema, const char** all_key_paths_begin, const char** all_key_paths_end) { KeyPathArray resolved_key_path_array; @@ -99,7 +99,20 @@ static KeyPathArray create_key_path_array(const ObjectSchema& object_schema, con } prop = schema_at_index->property_for_public_name(property); if (prop) { - resolved_key_path.emplace_back(schema_at_index->table_key, prop->column_key); + if ((to_underlying(prop->type & PropertyType::LinkingObjects)) != 0) { + // TODO Why isn't this just filled in the PropertyInfo::column_key? + auto originClass = schema.find(prop->object_type); + if (originClass != schema.end()) { + ColKey forwardColKey = originClass->property_for_name(prop->link_origin_property_name)->column_key; + ConstTableRef originTable = group.get_table(originClass->table_key); + resolved_key_path.emplace_back(schema_at_index->table_key, originTable->get_opposite_column(forwardColKey)); + } else { + throw InvalidArgument(util::format("Property '%1' in Keypath '%2' is a backlink that points to a class that could not be found: %3", + property, *it, prop->object_type)); + } + } else { + resolved_key_path.emplace_back(schema_at_index->table_key, prop->column_key); + } schema_at_index = nullptr; } else { @@ -121,9 +134,10 @@ RLM_API realm_key_path_array_t* realm_create_key_path_array(const realm_t* realm return wrap_err([&]() { KeyPathArray ret; if (user_key_paths) { + const Group& group = (*realm)->read_group(); const Schema& schema = (*realm)->schema(); const ObjectSchema& object_schema = schema_for_table(*realm, TableKey(object_class_key)); - ret = create_key_path_array(object_schema, schema, user_key_paths, user_key_paths + user_key_paths_count); + ret = create_key_path_array(group, object_schema, schema, user_key_paths, user_key_paths + user_key_paths_count); } return new realm_key_path_array_t(std::move(ret)); });