Skip to content

Commit

Permalink
fixed FirstOrCreate not handled error when table is not exists (#5367)
Browse files Browse the repository at this point in the history
* fixed FirstOrCreate not handled error when table is not exists

* delete useless part
  • Loading branch information
ophum committed May 28, 2022
1 parent 7e13b03 commit dc1ae39
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions finisher_api.go
Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions tests/create_test.go
Expand Up @@ -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)
Expand Down

0 comments on commit dc1ae39

Please sign in to comment.