Add ErrPrimaryKeyRequired if schema has no primary key defined

This commit is contained in:
Jinzhu 2021-01-06 14:42:42 +08:00
parent 435bf70865
commit 5e72cd9a2b
1 changed files with 7 additions and 2 deletions

View File

@ -178,10 +178,15 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat
break break
} else { } else {
resultsValue := reflect.Indirect(reflect.ValueOf(dest)) resultsValue := reflect.Indirect(reflect.ValueOf(dest))
if result.Statement.Schema.PrioritizedPrimaryField == nil {
tx.AddError(ErrPrimaryKeyRequired)
break
} else {
primaryValue, _ := result.Statement.Schema.PrioritizedPrimaryField.ValueOf(resultsValue.Index(resultsValue.Len() - 1)) primaryValue, _ := result.Statement.Schema.PrioritizedPrimaryField.ValueOf(resultsValue.Index(resultsValue.Len() - 1))
queryDB = tx.Clauses(clause.Gt{Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey}, Value: primaryValue}) queryDB = tx.Clauses(clause.Gt{Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey}, Value: primaryValue})
} }
} }
}
tx.RowsAffected = rowsAffected tx.RowsAffected = rowsAffected
return tx return tx