From 5e72cd9a2b276c0addc5f102b0a444798481576a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 6 Jan 2021 14:42:42 +0800 Subject: [PATCH] Add ErrPrimaryKeyRequired if schema has no primary key defined --- finisher_api.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/finisher_api.go b/finisher_api.go index 03bcd20f..73424dc2 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -178,8 +178,13 @@ func (db *DB) FindInBatches(dest interface{}, batchSize int, fc func(tx *DB, bat break } else { resultsValue := reflect.Indirect(reflect.ValueOf(dest)) - 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}) + if result.Statement.Schema.PrioritizedPrimaryField == nil { + tx.AddError(ErrPrimaryKeyRequired) + break + } else { + 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}) + } } }