From dc1ae394f329340cb4475b037fe9f98bdbf7176d Mon Sep 17 00:00:00 2001 From: "t-inagaki@hum_op" Date: Sat, 28 May 2022 23:18:43 +0900 Subject: [PATCH] fixed FirstOrCreate not handled error when table is not exists (#5367) * fixed FirstOrCreate not handled error when table is not exists * delete useless part --- finisher_api.go | 4 ++-- tests/create_test.go | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index da4ef8f7..7a3f27ba 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -351,9 +351,9 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) { } return tx.Model(dest).Updates(assigns) - } else { - tx.Error = result.Error } + } else { + tx.Error = result.Error } return tx } diff --git a/tests/create_test.go b/tests/create_test.go index 3730172f..274a7f48 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -476,6 +476,13 @@ func TestOmitWithCreate(t *testing.T) { CheckUser(t, result2, user2) } +func TestFirstOrCreateNotExistsTable(t *testing.T) { + company := Company{Name: "first_or_create_if_not_exists_table"} + if err := DB.Table("not_exists").FirstOrCreate(&company).Error; err == nil { + t.Errorf("not exists table, but err is nil") + } +} + func TestFirstOrCreateWithPrimaryKey(t *testing.T) { company := Company{ID: 100, Name: "company100_with_primarykey"} DB.FirstOrCreate(&company)