Skip to content

Commit

Permalink
fix(i): Require secondary id field for one-ones (#1770)
Browse files Browse the repository at this point in the history
## Relevant issue(s)

Resolves #1769

## Description

Require secondary id field for one-ones.

Simple silly bug caused by developer misunderstanding. Simple fix.
  • Loading branch information
AndrewSisley authored Aug 10, 2023
1 parent 5034cdc commit 480f1a9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,9 @@ func validateUpdateCollectionFields(
if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
return false, NewErrPrimarySideOnMany(proposedField.Name)
}
}

if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT {
idFieldName := proposedField.Name + "_id"
idField, idFieldFound := proposedDesc.Schema.GetField(idFieldName)
if !idFieldFound {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,38 @@ func TestSchemaUpdatesAddFieldKindForeignObject_RelatedRelationTypeMismatch(t *t
testUtils.ExecuteTestCase(t, test)
}

func TestSchemaUpdatesAddFieldKindForeignObject_MissingSecondaryID(t *testing.T) {
test := testUtils.TestCase{
Description: "Test schema update, add field with kind foreign object (16), valid, functional",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.SchemaPatch{
Patch: `
[
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {
"Name": "foo", "Kind": 16, "RelationType": 133, "Schema": "Users", "RelationName": "foo"
}},
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {
"Name": "foo_id", "Kind": 1, "RelationType": 64, "RelationName": "foo"
}},
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {
"Name": "foobar", "Kind": 16, "RelationType": 5, "Schema": "Users", "RelationName": "foo"
}}
]
`,
ExpectedError: "missing id field for relation object field. Field: foobar, ExpectedIDFieldName: foobar_id",
},
},
}
testUtils.ExecuteTestCase(t, test)
}

func TestSchemaUpdatesAddFieldKindForeignObject_Succeeds(t *testing.T) {
key1 := "bae-decf6467-4c7c-50d7-b09d-0a7097ef6bad"

Expand All @@ -486,6 +518,9 @@ func TestSchemaUpdatesAddFieldKindForeignObject_Succeeds(t *testing.T) {
}},
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {
"Name": "foobar", "Kind": 16, "RelationType": 5, "Schema": "Users", "RelationName": "foo"
}},
{ "op": "add", "path": "/Users/Schema/Fields/-", "value": {
"Name": "foobar_id", "Kind": 1, "RelationType": 64, "RelationName": "foo"
}}
]
`,
Expand Down

0 comments on commit 480f1a9

Please sign in to comment.