Skip to content

Commit

Permalink
Only (re) add primary field if it hasnt already been created
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Jun 28, 2024
1 parent a428df4 commit 0015887
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
23 changes: 13 additions & 10 deletions internal/request/graphql/schema/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 47 additions & 1 deletion tests/integration/schema/one_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ package schema
import (
"testing"

"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

Expand All @@ -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"),
},
},
},
},
},
},
}
Expand Down

0 comments on commit 0015887

Please sign in to comment.