fixed FirstOrCreate not handled error when table is not exists (#5367)

* fixed FirstOrCreate not handled error when table is not exists

* delete useless part
This commit is contained in:
t-inagaki@hum_op 2022-05-28 23:18:43 +09:00 committed by GitHub
parent 7e13b03bd4
commit dc1ae394f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 2 deletions

View File

@ -351,9 +351,9 @@ func (db *DB) FirstOrCreate(dest interface{}, conds ...interface{}) (tx *DB) {
} }
return tx.Model(dest).Updates(assigns) return tx.Model(dest).Updates(assigns)
} else {
tx.Error = result.Error
} }
} else {
tx.Error = result.Error
} }
return tx return tx
} }

View File

@ -476,6 +476,13 @@ func TestOmitWithCreate(t *testing.T) {
CheckUser(t, result2, user2) 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) { func TestFirstOrCreateWithPrimaryKey(t *testing.T) {
company := Company{ID: 100, Name: "company100_with_primarykey"} company := Company{ID: 100, Name: "company100_with_primarykey"}
DB.FirstOrCreate(&company) DB.FirstOrCreate(&company)