fix:throw model value required error

This commit is contained in:
a631807682 2023-02-06 14:28:44 +08:00
parent e1f46eb802
commit 04af65df35
No known key found for this signature in database
GPG Key ID: 137D1D75522168AB
3 changed files with 16 additions and 1 deletions

View File

@ -7,7 +7,7 @@ import (
func RowQuery(db *gorm.DB) {
if db.Error == nil {
BuildQuerySQL(db)
if db.DryRun {
if db.DryRun || db.Error != nil {
return
}

View File

@ -120,6 +120,8 @@ func (stmt *Statement) QuoteTo(writer clause.Writer, field interface{}) {
write(v.Raw, stmt.Schema.PrioritizedPrimaryField.DBName)
} else if len(stmt.Schema.DBNames) > 0 {
write(v.Raw, stmt.Schema.DBNames[0])
} else {
stmt.DB.AddError(ErrModelValueRequired)
}
} else {
write(v.Raw, v.Name)

View File

@ -1366,3 +1366,16 @@ func TestQueryResetNullValue(t *testing.T) {
AssertEqual(t, q1, qs[0])
AssertEqual(t, q2, qs[1])
}
func TestQueryError(t *testing.T) {
type P struct{}
var p1 P
err := DB.Take(&p1, 1).Error
AssertEqual(t, err, gorm.ErrModelValueRequired)
var p2 interface{}
err = DB.Table("ps").Clauses(clause.Eq{Column: clause.Column{
Table: clause.CurrentTable, Name: clause.PrimaryKey}, Value: 1}).Scan(&p2).Error
AssertEqual(t, err, gorm.ErrModelValueRequired)
}