From e19dac065e23be694b9b4fdbde7fce4ad676e65f Mon Sep 17 00:00:00 2001 From: demoManito <1430482733@qq.com> Date: Mon, 26 Sep 2022 23:01:38 +0800 Subject: [PATCH 1/2] fix maybe nil panic --- schema/relationship.go | 3 ++- tests/callbacks_test.go | 3 +++ tests/transaction_test.go | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/schema/relationship.go b/schema/relationship.go index bb8aeb649..72e8ab4e0 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -160,7 +160,8 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi primaryKeyField := schema.PrioritizedPrimaryField if len(relation.foreignKeys) > 0 { - if primaryKeyField = schema.LookUpField(relation.foreignKeys[0]); primaryKeyField == nil || len(relation.foreignKeys) > 1 { + primaryKeyField = schema.LookUpField(relation.foreignKeys[0]) + if primaryKeyField == nil || len(relation.foreignKeys) > 1 { schema.err = fmt.Errorf("invalid polymorphic foreign keys %+v for %v on field %s", relation.foreignKeys, schema, field.Name) } } diff --git a/tests/callbacks_test.go b/tests/callbacks_test.go index 2bf9496b9..4479da4c0 100644 --- a/tests/callbacks_test.go +++ b/tests/callbacks_test.go @@ -113,6 +113,9 @@ func TestCallbacks(t *testing.T) { for idx, data := range datas { db, err := gorm.Open(nil, nil) + if err != nil { + t.Fatal(err) + } callbacks := db.Callback() for _, c := range data.callbacks { diff --git a/tests/transaction_test.go b/tests/transaction_test.go index 0ac04a047..5872da94a 100644 --- a/tests/transaction_test.go +++ b/tests/transaction_test.go @@ -102,7 +102,7 @@ func TestTransactionWithBlock(t *testing.T) { return errors.New("the error message") }) - if err.Error() != "the error message" { + if err != nil && err.Error() != "the error message" { t.Fatalf("Transaction return error will equal the block returns error") } From 69a685b7f573c2d2ce616d4336b6432efdbb7a0a Mon Sep 17 00:00:00 2001 From: demoManito <1430482733@qq.com> Date: Mon, 26 Sep 2022 23:02:27 +0800 Subject: [PATCH 2/2] reset code --- schema/relationship.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/schema/relationship.go b/schema/relationship.go index 72e8ab4e0..bb8aeb649 100644 --- a/schema/relationship.go +++ b/schema/relationship.go @@ -160,8 +160,7 @@ func (schema *Schema) buildPolymorphicRelation(relation *Relationship, field *Fi primaryKeyField := schema.PrioritizedPrimaryField if len(relation.foreignKeys) > 0 { - primaryKeyField = schema.LookUpField(relation.foreignKeys[0]) - if primaryKeyField == nil || len(relation.foreignKeys) > 1 { + if primaryKeyField = schema.LookUpField(relation.foreignKeys[0]); primaryKeyField == nil || len(relation.foreignKeys) > 1 { schema.err = fmt.Errorf("invalid polymorphic foreign keys %+v for %v on field %s", relation.foreignKeys, schema, field.Name) } }