Skip to content

Conversation

@phongphan
Copy link
Contributor

@phongphan phongphan commented Jul 25, 2025

When using returning clauses in conjunction with on conflicted do update must be considered as ScanUpdate.

  • Do only one thing
  • Non-breaking API changes
  • Tested

What did this pull request do?

This should fix #7538

User Case Description

When using CreateInBatches with Returning{} in conjunction with ON CONFLICTED DO UPDATE, it will either crash with panic, or run successfully without updating the object.

The test will fails on both cases.

Panic example:

2025/07/25 19:07:38 testing sqlite3...
=== RUN   TestGORM
--- FAIL: TestGORM (0.00s)
panic: reflect: reflect.Value.SetLen using unaddressable value [recovered]
	panic: reflect: reflect.Value.SetLen using unaddressable value

goroutine 5 [running]:
testing.tRunner.func1.2({0x10342bae0, 0xc000045d70})
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/testing/testing.go:1734 +0x2bc
testing.tRunner.func1()
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/testing/testing.go:1737 +0x47c
panic({0x10342bae0?, 0xc000045d70?})
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/runtime/panic.go:792 +0x124
reflect.flag.mustBeAssignableSlow(0x97)
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/reflect/value.go:260 +0x88
reflect.flag.mustBeAssignable(...)
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/reflect/value.go:247
reflect.Value.SetLen({0x103418d40?, 0xc00000e018?, 0xc0000113aa?}, 0x0)
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/reflect/value.go:2157 +0x4c
gorm.io/gorm.Scan({0x103543868, 0xc000359540}, 0xc00042d9b0, 0x0)
	/Users/phongphan/gorm-playground/gorm/scan.go:304 +0xd74
gorm.io/gorm/callbacks.RegisterDefaultCallbacks.Create.func3(0xc00042d9b0)
	/Users/phongphan/gorm-playground/gorm/callbacks/create.go:95 +0x764
gorm.io/gorm.(*processor).Execute(0xc00031d1d0, 0xc00042d9b0)
	/Users/phongphan/gorm-playground/gorm/callbacks.go:130 +0x7fc
gorm.io/gorm.(*DB).CreateInBatches.func1(...)
	/Users/phongphan/gorm-playground/gorm/finisher_api.go:50
gorm.io/gorm.(*DB).CreateInBatches(0xc00042d920, {0x103418d80, 0xc0001adf68}, 0x2)
	/Users/phongphan/gorm-playground/gorm/finisher_api.go:60 +0x4a8
gorm.io/playground.TestGORM(0xc000003c00)
	/Users/phongphan/gorm-playground/main_test.go:22 +0x418
testing.tRunner(0xc000003c00, 0x1035345e0)
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/testing/testing.go:1792 +0x184
created by testing.(*T).Run in goroutine 1
	/opt/homebrew/Cellar/go/1.24.5/libexec/src/testing/testing.go:1851 +0x688
FAIL	gorm.io/playground	0.387s
FAIL

This PR add the RETURNING with ON CONFLICTED DO UPDATE as ScanUpdate.

@jinzhu
Copy link
Member

jinzhu commented Jul 28, 2025

Hi @phongphan,
Thanks for the fix! Could you please add some tests to help prevent this issue from happening again in the future? Thank you.

@phongphan phongphan force-pushed the fix/inserted-in-batches-panic branch from 142e213 to 02da158 Compare July 28, 2025 14:28
@phongphan
Copy link
Contributor Author

@jinzhu sure, updated!

@jinzhu jinzhu merged commit eb90a02 into go-gorm:master Jul 29, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CreateInBatches panic when using with Returning{}

2 participants