fixed FirstOrCreate not handled error when table is not exists

This commit is contained in:
ophum 2022-05-23 18:38:52 +09:00
parent 7d1a92d60e
commit 8edc482525
2 changed files with 9 additions and 0 deletions

View File

@ -354,6 +354,8 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
} else {
tx.Error = result.Error
}
} else {
tx.Error = result.Error
}
return tx
}

View File

@ -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)