Fixed a critical bug where has_one and has_many cross-schema relationships were not setting relationship linkage data (relationships.<name>.data) in JSON:API responses, even though the related resources were correctly included in the included section.
Changes to handle_cross_schema_included:
- Added support for Array sources (not just Hash)
- Converts Array of ResourceFragments/ResourceIdentities to Hash
- Passes
source_fragmentsin enhanced_options to downstream handlers
Changes to handle_cross_schema_to_one:
- Added linkage setting after creating related resource fragment
- Calls
add_related_identityon source fragment to populaterelationships.data - Added comprehensive debug logging
Changes to handle_cross_schema_to_many:
- Fixed SQL query to use WHERE clause with foreign_key
- Groups related records by source_id for proper linkage
- Adds linkage for each related resource to source fragment
Comprehensive unit test suite covering:
has_one tests:
test_has_one_cross_schema_creates_linkage_data- Basic linkage creationtest_has_one_cross_schema_with_null_foreign_key- Null foreign key handlingtest_cross_schema_relationship_with_array_source- Array source supporttest_cross_schema_relationship_with_hash_source- Hash source supporttest_multiple_candidates_with_same_recruiter- Deduplicationtest_cross_schema_included_in_full_serialization- End-to-end testtest_cross_schema_relationships_hash_registration- Configuration testtest_non_cross_schema_relationships_still_work- Regression test
has_many tests:
test_has_many_cross_schema_creates_linkage_data- Basic has_many linkagetest_has_many_cross_schema_with_array_source- Array source with has_manytest_has_many_cross_schema_empty_collection- Empty collection handlingtest_has_many_cross_schema_deduplication- Deduplication across multiple sources
Integration test placeholders (skipped - require full Rails setup)
Added tables for cross-schema testing:
test_candidates- Primary resource with foreign keystest_users- Related resource from "different schema"test_locations- Normal same-schema relationshiptest_departments- For has_one cross-schematest_companies- For has_many cross-schema
test/fixtures/test_users.ymltest/fixtures/test_candidates.ymltest/fixtures/test_locations.ymltest/fixtures/test_departments.yml
Complete documentation covering:
- Problem description with examples
- Root cause analysis
- Solution implementation details
- Usage examples
- Testing approach
- Migration notes
The fix was tested and verified to work in production use cases where:
- Primary resources have
has_onerelationships to resources in different PostgreSQL schemas - Relationship linkage data now correctly appears in JSON:API responses
- Frontend deserializers can properly link related resources
{
"data": {
"relationships": {
"author": { "data": null }
}
},
"included": [
{ "type": "users", "id": "123" }
]
}{
"data": {
"relationships": {
"author": {
"data": { "type": "users", "id": "123" }
}
}
},
"included": [
{ "type": "users", "id": "123" }
]
}This fix is fully backward compatible. Existing code using cross-schema relationships will automatically benefit from the fix without any changes required.
- Run full test suite to ensure no regressions
- Create PR to main repository
- Update version number
- Release new gem version