“Invalid field found for struct” error in Go

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật