Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use interfaces type with Gorm? #6972

Open
Twsouza opened this issue Apr 17, 2024 · 0 comments
Open

How to use interfaces type with Gorm? #6972

Twsouza opened this issue Apr 17, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@Twsouza
Copy link

Twsouza commented Apr 17, 2024

Your Question

I'm trying to pass an slice of interface to the Create method but it returns an error:

"unsupported data type: [interfaces.CacheType](interfaces.CacheType): Table not set, please set it like: db.Model(&user) or db.Table(\"users\")"

The CacheType is an interface defined as follow:

type CacheType interface {
	Map(data []byte) error
	Initialize() interface{}
	GetCacheVersion() string
}

The array will have the same struct passed into it at one time and is expected to apply to the same table for each object in it. There are many different caches though which is why I am using an interface rather than a concrete type. I tried to use db.Model passing the struct and also the db.Table passing the table name but it received a panic in gorm's code.

goroutine 242 [running]:
gorm.io/gorm/callbacks.ConvertToCreateValues(0xc0004ea380)
	/home/twsouza/go/pkg/mod/gorm.io/gorm@v1.25.7/callbacks/create.go:237 +0x596
gorm.io/gorm/callbacks.Create.func1(0xc0004ee5a0)
	/home/twsouza/go/pkg/mod/gorm.io/gorm@v1.25.7/callbacks/create.go:66 +0x1ea
gorm.io/gorm.(*processor).Execute(0xc000116a50, 0xc0004ee5a0)
	/home/twsouza/go/pkg/mod/gorm.io/gorm@v1.25.7/callbacks.go:130 +0x67c
gorm.io/gorm.(*DB).Create(0xc0004ee5a0, {0x174c900, 0xc0005b20c0})
	/home/twsouza/go/pkg/mod/gorm.io/gorm@v1.25.7/finisher_api.go:24 +0xf4
bitbucket.org/organization/repo/db/dbgorm.(*GormDAO).Upsert(0xc0004ee4b0, {0x1abb1c0, 0xc0004ef2c0}, {0x174c900, 0xc0005b20c0})
	/home/twsouza/src/repo/db/dbgorm/dbgorm.go:92 +0x1f9
bitbucket.org/organization/repo/cachehelper.(*DbCache[...]).Seed(0x1ad4420, {0x1abb1c0, 0xc0004ef2c0}, {0xc0000609c0, 0x3, 0x4}, {0x1ad1e80, 0xc0004ef4d0}, {0x19ad3be, 0xe})
	/home/twsouza/src/repo/cachehelper/db-cacheable.go:71 +0x491
bitbucket.org/organization/repo/cachehelper.Cache[...].MapAndSeed(0x1acd580, {0x1abb1c0, 0xc0004ef2c0}, 0x1, {0xc0000d0570, 0x16}, {0x1ad1e80, 0xc0004ef4d0})
	/home/twsouza/src/repo/cachehelper/cache.go:242 +0x1718
bitbucket.org/organization/repo/cachehelper.FindAndSeedUnseededCaches.func2(0x1, {0x1abffc0, 0xc000537b80}, 0xc0000556e0)
	/home/twsouza/src/repo/cachehelper/cache.go:460 +0x39b
created by bitbucket.org/organization/repo/cachehelper.FindAndSeedUnseededCaches in goroutine 192
	/home/twsouza/src/repo/cachehelper/cache.go:444 +0xe4c

GormDAO is an implementation of an orm interface that uses Gorm.
It should be generic, so we don't know the type until the runtime.
How can I set the table name using Gorm?

The document you expected this should be explained

I didn't find any documentation explaining how to use interface with Gorm.

Expected answer

Documentation about how to use interfaces with Gorm.

@Twsouza Twsouza added the type:question general questions label Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants