fix: panic when using clause.Returning with CreateInBatches (#7768)
* fix: make CreateInBatches compatible with clause.Returning by using addressable batch slice
reflectValue.Slice(i, ends).Interface() returns an unaddressable sub-slice.
When clause.Returning{} attempts to call SetLen on it via reflection during
the create callback, Go runtime panics with:
"panic: reflect: reflect.Value.SetLen using unaddressable value"
Fix by allocating a new addressable *[]T for each batch and passing it as
Statement.Dest instead. After execution, sync the results back into the
original slice so callers receive server-generated field values (e.g.
updated_at, created_at, id) populated by RETURNING.
Reproduces when:
- gorm.Config.CreateBatchSize > 0, OR
- db.CreateInBatches() is called directly
- AND clause.Returning{} is used
- AND the database supports RETURNING (SQLite >= 3.35.0, PostgreSQL)
* add test
* fix: implement Create method to handle batch creation and prevent panic on unaddressable slice
---------
Co-authored-by: Khang Truong Quang <khang.tq@teko.vn> T
Trương Quang Khang committed
2a22022d4147f01763bd4804fdfe322b43ca3b55
Parent: 3322929
Committed by GitHub <noreply@github.com>
on 5/11/2026, 9:44:42 AM