GORM Playground Link
no
gorm version
gorm.io/driver/sqlite v1.5.3
gorm.io/gorm v1.25.4
Description
When using has many Foreign Key, i can create a reverse field by refereneces
type Disk struct {
UUID string `gorm:"primaryKey"`
Instance *Instance `gorm:"references:Name"`
InstanceName *string
}
type Instance struct {
UUID string `gorm:"primaryKey"`
Name string
Disk []Disk `gorm:"foreignKey:InstanceName;References:Name"`
}
But if i change the InstanceName to another one like IntanceOwner, it wont work
type Disk struct {
UUID string `gorm:"primaryKey"`
Instance *Instance `gorm:"references:Name"` // <-- comment out this works
InstanceOwner *string // <-- Name change from InstanceName to InstanceOwner
}
type Instance struct {
UUID string `gorm:"primaryKey"`
Name string
Disk []Disk `gorm:"foreignKey:InstanceOwner;References:Name"`
}
func main() {
db, err := gorm.Open(sqlite.Open("a.db?_foreign_keys=on"), &gorm.Config{})
if err != nil { panic(err) }
err = db.AutoMigrate(&Instance{}, &Disk{})
if err != nil { panic(err) }
}
will got following erro
023/09/13 21:04:28 ./nok.go:23
[error] invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] failed to parse value &main.Instance{UUID:"", Name:"", Disk:[]main.Disk(nil)}, got error invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] failed to parse value &main.Disk{UUID:"", Instance:(*main.Instance)(nil), InstanceOwner:(*string)(nil)}, got error invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] failed to parse value &main.Disk{UUID:"", Instance:(*main.Instance)(nil), InstanceOwner:(*string)(nil)}, got error invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
2023/09/13 21:04:28 ./nok.go:23
[error] invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
panic: invalid field found for struct main.Disk's field Instance: define a valid foreign key for relations or implement the Valuer/Scanner interface
goroutine 1 [running]:
main.main()
./nok.go:24 +0x176
exit status 2
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨