framework icon indicating copy to clipboard operation
framework copied to clipboard

Add WhereHas method

Open zoryamba opened this issue 5 months ago • 2 comments

📑 Description

Closes https://github.com/goravel/goravel/issues/373

✅ Checks

  • [ ] Added test cases for my code

zoryamba avatar Oct 27 '25 19:10 zoryamba

@hwbrzzl fixed comments. Added some tests for whereHas method. But have an issue testing buildWhereRelation method. It uses getModelSchema function, which creates gormio.Statement instance. does it require db connection to detect schema relationships? Here is error stack I get

=== RUN   TestBuildWhereRelation
--- FAIL: TestBuildWhereRelation (7.42s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x100aae2ec]

goroutine 21 [running]:
testing.tRunner.func1.2({0x1012ae060, 0x1018aead0})
	/Users/zoryamba/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1872 +0x3a4
testing.tRunner.func1()
	/Users/zoryamba/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1875 +0x4a8
panic({0x1012ae060?, 0x1018aead0?})
	/Users/zoryamba/go/pkg/mod/golang.org/[email protected]/src/runtime/panic.go:783 +0xec
sync.(*Map).Load(0x0, {0x101336280, 0x101308b00})
	/Users/zoryamba/go/pkg/mod/golang.org/[email protected]/src/sync/hashtriemap.go:50 +0x3c
gorm.io/gorm/schema.ParseWithSpecialTableName({0x101263ae0, 0x14000094960}, 0x0, {0x0, 0x0}, {0x0, 0x0})
	/Users/zoryamba/Work/own/goravel/gorm/schema/schema.go:163 +0x418
gorm.io/gorm.(*Statement).ParseWithSpecialTableName(0x1400024c1e0, {0x101263ae0, 0x14000094960}, {0x0, 0x0})
	/Users/zoryamba/Work/own/goravel/gorm/statement.go:517 +0xa8
gorm.io/gorm.(*Statement).Parse(0x1400024c1e0, {0x101263ae0, 0x14000094960})
	/Users/zoryamba/Work/own/goravel/gorm/statement.go:513 +0x44
github.com/goravel/framework/database/gorm.getModelSchema({0x101263ae0, 0x14000094960}, 0x14000334db0)
	/Users/zoryamba/Work/own/goravel/goravel-framework/database/gorm/utils.go:36 +0xe0
github.com/goravel/framework/database/gorm.(*Query).relationSubquery(0x14000197408, {0x1011a6609, 0x5}, {0x1013783f0, 0x14000197688})
	/Users/zoryamba/Work/own/goravel/goravel-framework/database/gorm/query.go:1434 +0x84
github.com/goravel/framework/database/gorm.(*Query).buildWhereRelation(0x14000197408, {{0x101341ac0, 0x14000197688}, {0x0, 0x0, 0x0}, 0x4, {0x1011a6609, 0x5}, 0x0, ...})
	/Users/zoryamba/Work/own/goravel/goravel-framework/database/gorm/query.go:1416 +0x3b4
github.com/goravel/framework/database/gorm.TestBuildWhereRelation(0x14000083180)
	/Users/zoryamba/Work/own/goravel/goravel-framework/database/gorm/query_test.go:159 +0x344
testing.tRunner(0x14000083180, 0x101368ac0)
	/Users/zoryamba/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1934 +0x140
created by testing.(*T).Run in goroutine 1
	/Users/zoryamba/go/pkg/mod/golang.org/[email protected]/src/testing/testing.go:1997 +0x74c

Debugger finished with the exit code 0

Should I test it with Docker under https://github.com/goravel/framework/tree/master/tests folder? Or is there any other way to get model schema without connection?

zoryamba avatar Nov 17 '25 23:11 zoryamba

@zoryamba Thanks, and yes, the test cases can be added in the tests folder if they need a real db connection.

hwbrzzl avatar Nov 18 '25 01:11 hwbrzzl