Skip to content

Commit

Permalink
Handle explicit nil on secondary side relations
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 4, 2024
1 parent 71e0779 commit 7508cb9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
5 changes: 5 additions & 0 deletions internal/db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,11 @@ func (c *collection) save(

relationFieldDescription, isSecondaryRelationID := fieldDescription.GetSecondaryRelationField(c.Definition())
if isSecondaryRelationID {
if val.Value() == nil {
// If the value (relation) is nil, we don't need to check for any documents already linked to it
continue
}

primaryId := val.Value().(string)

err = c.patchPrimaryDoc(
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ func getEventsForCreateDoc(s *state, action CreateDoc) map[string]struct{} {

// check for any secondary relation fields that could publish an event
for f, v := range doc.Values() {
if v.Value() == nil {
// If the new relation value is nil there will be no related document
// to get an event for
continue
}

field, ok := def.GetFieldByName(f.Name())
if !ok {
continue // ignore unknown field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ package one_to_one
import (
"testing"

"github.com/stretchr/testify/require"

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

Expand Down Expand Up @@ -119,10 +117,33 @@ func TestMutationCreateOneToOne_WithExplicitNullOnSecondarySide(t *testing.T) {
"published": testUtils.NewDocIndex(0, 0),
},
},
testUtils.Request{
Request: `
query {
Book {
name
author {
name
}
}
}`,
Results: map[string]any{
"Book": []map[string]any{
{
"name": "Secrets at Maple Syrup Farm",
"author": nil,
},
{
"name": "How to Be a Canadian",
"author": map[string]any{
"name": "Will Ferguson",
},
},
},
},
},
},
}

require.Panics(t, func() {
testUtils.ExecuteTestCase(t, test)
})
testUtils.ExecuteTestCase(t, test)
}

0 comments on commit 7508cb9

Please sign in to comment.