invalid field found for struct ()’s field Enclosure: define a valid foreign key for relations or implement the Valuer/Scanner interface
I’ve been getting this error in the project I’ve coded with Go, and I haven’t been able to figure out the source of the problem for a long time.
In the project where I perform database operations using GORM, I get this error when trying to fetch data from relational tables. The GORM query causing the error is:
// GET ALL ENCLOSURES
func (enclosures *Enclosure) GetAllEnclosures(db *gorm.DB, companyId uint) ([]struct {
Enclosure Enclosure
MapRegionName string
}, error) {
var enclosuresList []struct {
Enclosure Enclosure
MapRegionName string
}
result := db.Table("enclosures").
Select("enclosures.*, map_regions.name as map_region_name").
Joins("LEFT JOIN map_regions ON map_regions.id = enclosures.map_region_fk").
Where("enclosures.company_fk = ? AND enclosures.deleted_at IS NULL", companyId).
Order("enclosures.created_at DESC").
Scan(&enclosuresList)
if result.Error != nil {
return nil, result.Error
}
return enclosuresList, nil
}
With this function, I want to join the map_region_fk in the enclosures table with the map_regions table to add the map region name to the query output. “When I run this function, I get the error: ‘invalid field found for struct ()’s field Enclosure: define a valid foreign key for relations or implement the Valuer/Scanner interface.'”
Enclosure Struct:
type Enclosure struct {
gorm.Model
ID uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
Description string `gorm:"not null"`
Period int64 `gorm:"not null"`
CompanyFk uint `gorm:"not null"`
Company Company `gorm:"foreignKey:CompanyFk;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
MaxSenors int64 `gorm:"default:20"`
Latitude float64 `gorm:"not null"`
Longitude float64 `gorm:"not null"`
MapRegionFk uint `gorm:"not null"`
MapRegion MapRegion `gorm:"foreignKey:MapRegionFk;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Duration int64 `gorm:"not null"`
Status int16 `gorm:"default:1"`
CreatedAt time.Time `gorm:"default:current_timestamp"`
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
MapRegion Struct:
type MapRegion struct {
gorm.Model
ID uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
MapFk uint `gorm:"not null"`
Map Map `gorm:"foreignKey:MapFk;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
MaxEnclosure int64 `gorm:"default:10"`
Status int16 `gorm:"default:1"`
CreatedAt time.Time `gorm:"default:current_timestamp"`
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
Map Struct:
type Map struct {
gorm.Model
ID uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
CompanyFk uint `gorm:"not null"`
Company Company `gorm:"foreignKey:CompanyFk"`
MaxRegion int64 `gorm:"default:5"`
Status int16 `gorm:"default:1"`
MapRegions []MapRegion `gorm:"foreignKey:MapFk;references:ID"`
CreatedAt time.Time `gorm:"default:current_timestamp"`
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
Company Struct:
type Company struct {
gorm.Model
ID uint `gorm:"primaryKey"`
Name string `gorm:"not null"`
Email string `gorm:"not null;unique"`
PhoneNumber string `gorm:"not null"`
UserID int64 `gorm:"not null"`
CountryFk int64 `gorm:"not null"`
Country models.Country `gorm:"foreignKey:CountryFk;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CityFk int64 `gorm:"not null"`
City models.City `gorm:"foreignKey:CityFk;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
TaxOffice string `gorm:"not null"`
TaxId string `gorm:"not null"`
Status int16
CreatedAt time.Time `gorm:"default:current_timestamp"`
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
I defined references for foreign keys in every struct I created, but I still kept encountering the same issue. I also tried dropping and recreating all the tables, but that didn’t solve the problem either.