假設(shè)我有 3 個結(jié)構(gòu),格式如下type Employee struct { Id int Name string CompanyId int `gorm:"column:companyId"` Company Company `gorm:"foreignKey:CompanyId"`}type Company struct { Id int CompanyName string OwnerId `gorm:"column:owner"` Owner Owner `gorm:"foreignKey:OwnerId"`}type Owner struct { Id int Name string Age int Email string}func (E Employee) GetAllEmployees() ([]Employee, error) { Employees := []Employee db.Preload("Company").Find(&Employees)}// -- -- There response will be like[ { id: 1 name: "codernadir" company: { id: 5 company_name: "Company" owner: { id 0 Name "" Age 0 Email "" } } }]在這里,我使用默認(rèn)值獲取 Owner 值。給定的例子是為了描述我想要達(dá)到的目標(biāo)。當(dāng)我加載員工時,我需要一種方法如何加載 Owner 結(jié)構(gòu)及其值?任何建議將不勝感激,并在此先感謝
GoLang 如何使用 GORM 加載嵌套對象
慕尼黑5688855
2023-01-03 10:08:12