'GORM Promoter: define a valid foreign key for relations or implement the Valuer/Scanner interface
I have been trying to use GORM foreign keys and I have tried everything but I keep getting the error Promoter: define a valid foreign key for relations or implement the Valuer/Scanner interface.
Here is my code:
type Site struct {
gorm.Model
ID int64 `gorm:"primaryKey"`
Domain string
Theme string
Promoter Promoter `gorm:"foreignKey:PromoterID;references:ID"`
}
type Promoter struct {
gorm.Model
ID int64 `gorm:"primaryKey"`
Name string
Owners []User `gorm:"many2many:user_promoters;"`
}
Solution 1:[1]
All you need is to add the foreign key as a slice like this
type Site struct {
gorm.Model
Domain string
Theme string
Promoter []Promoter
}
After that, you need to add the foreignKey inside the Promoter struct like that
type Promoter struct {
gorm.Model
Name string
SiteID unit
Owners []User `gorm:"many2many:user_promoters;"`
}
As they mention here in the docs
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
