diff --git a/internal/request/graphql/schema/collection.go b/internal/request/graphql/schema/collection.go index c4180be0f4..d6ae0854d0 100644 --- a/internal/request/graphql/schema/collection.go +++ b/internal/request/graphql/schema/collection.go @@ -667,15 +667,13 @@ func finalizeRelations( continue } - var otherColFieldDescription immutable.Option[client.CollectionFieldDescription] - for _, otherField := range otherColDefinition.Value().Description.Fields { - if otherField.RelationName.Value() == field.RelationName.Value() { - otherColFieldDescription = immutable.Some(otherField) - break - } - } + otherColFieldDescription, hasOtherColFieldDescription := otherColDefinition.Value().Description.GetFieldByRelation( + field.RelationName.Value(), + definition.GetName(), + field.Name, + ) - if !otherColFieldDescription.HasValue() || otherColFieldDescription.Value().Kind.Value().IsArray() { + if !hasOtherColFieldDescription || otherColFieldDescription.Kind.Value().IsArray() { if _, exists := definition.Schema.GetFieldByName(field.Name); !exists { // Relations only defined on one side of the object are possible, and so if this is one of them // or if the other side is an array, we need to add the field to the schema (is primary side) diff --git a/tests/integration/schema/one_many_test.go b/tests/integration/schema/one_many_test.go index 69e1dace7f..6783a17e7b 100644 --- a/tests/integration/schema/one_many_test.go +++ b/tests/integration/schema/one_many_test.go @@ -94,7 +94,34 @@ func TestSchemaOneMany_SelfReferenceOneFieldLexographicallyFirst(t *testing.T) { b: [User] } `, - ExpectedError: "relation missing field. Object: User, RelationName: user_user", + ExpectedResults: []client.CollectionDescription{ + { + Name: immutable.Some("User"), + Fields: []client.CollectionFieldDescription{ + { + Name: "_docID", + }, + { + Name: "a", + ID: 1, + Kind: immutable.Some[client.FieldKind](client.ObjectKind("User")), + RelationName: immutable.Some("user_user"), + }, + { + Name: "a_id", + ID: 2, + Kind: immutable.Some[client.FieldKind](client.ScalarKind(client.FieldKind_DocID)), + RelationName: immutable.Some("user_user"), + }, + { + Name: "b", + ID: 3, + Kind: immutable.Some[client.FieldKind](client.ObjectArrayKind("User")), + RelationName: immutable.Some("user_user"), + }, + }, + }, + }, }, }, }