diff --git a/internal/request/graphql/schema/collection.go b/internal/request/graphql/schema/collection.go index 7c4e44593b..c4180be0f4 100644 --- a/internal/request/graphql/schema/collection.go +++ b/internal/request/graphql/schema/collection.go @@ -676,16 +676,19 @@ func finalizeRelations( } if !otherColFieldDescription.HasValue() || otherColFieldDescription.Value().Kind.Value().IsArray() { - // 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). - definition.Schema.Fields = append( - definition.Schema.Fields, - client.SchemaFieldDescription{ - Name: field.Name, - Kind: field.Kind.Value(), - Typ: cTypeByFieldNameByObjName[definition.Schema.Name][field.Name], - }, - ) + 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) + // if the field has not been explicitly declared by the user. + definition.Schema.Fields = append( + definition.Schema.Fields, + client.SchemaFieldDescription{ + Name: field.Name, + Kind: field.Kind.Value(), + Typ: cTypeByFieldNameByObjName[definition.Schema.Name][field.Name], + }, + ) + } } otherIsEmbedded := len(otherColDefinition.Value().Description.Fields) == 0 diff --git a/tests/integration/schema/one_many_test.go b/tests/integration/schema/one_many_test.go index 0f80537d25..bab84b3d40 100644 --- a/tests/integration/schema/one_many_test.go +++ b/tests/integration/schema/one_many_test.go @@ -13,6 +13,9 @@ package schema import ( "testing" + "github.com/sourcenetwork/immutable" + + "github.com/sourcenetwork/defradb/client" testUtils "github.com/sourcenetwork/defradb/tests/integration" ) @@ -30,7 +33,50 @@ func TestSchemaOneMany_Primary(t *testing.T) { owner: User @primary } `, - ExpectedError: "duplicate field. Name: owner", + ExpectedResults: []client.CollectionDescription{ + { + Name: immutable.Some("User"), + Fields: []client.CollectionFieldDescription{ + { + Name: "_docID", + }, + { + Name: "dogs", + ID: 1, + Kind: immutable.Some[client.FieldKind](client.ObjectArrayKind("Dog")), + RelationName: immutable.Some("dog_user"), + }, + { + Name: "name", + ID: 2, + }, + }, + }, + { + Name: immutable.Some("Dog"), + Fields: []client.CollectionFieldDescription{ + { + Name: "_docID", + }, + { + Name: "name", + ID: 1, + }, + { + Name: "owner", + ID: 2, + Kind: immutable.Some[client.FieldKind](client.ObjectKind("User")), + RelationName: immutable.Some("dog_user"), + }, + { + Name: "owner_id", + ID: 3, + Kind: immutable.Some[client.FieldKind](client.ScalarKind(client.FieldKind_DocID)), + RelationName: immutable.Some("dog_user"), + }, + }, + }, + }, }, }, }