diff --git a/finisher_api.go b/finisher_api.go index da4ef8f76..7a3f27bae 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 3730172fd..274a7f486 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)