英文:
Query Models through Gorm
问题
这是我的律师模型:
type Lawyer struct {
ID uint `gorm:"primaryKey" json:"id"`
FirstName string `gorm:"type:varchar(100) not null" json:"first_name"`
LastName string `gorm:"type:varchar(100) not null" json:"last_name"`
FullName string `gorm:"->;type:text GENERATED ALWAYS AS (concat(first_name,' ',last_name)) VIRTUAL;" json:"full_name"`
LocationID uint `gorm:"not null" json:"location_id"`
Location Location `gorm:"foreignKey:location_id" json:"location"`
Email string `gorm:"unique;not null" json:"email"`
Phone string `gorm:"type:varchar(100);not null" json:"phone"`
Password string `gorm:"type:varchar(100);not null" json:"password"`
ImageURL string `gorm:"type:text" json:"image_url"`
Education string `gorm:"not null" json:"education"`
Experience uint `gorm:"not null" json:"experience"`
PracticeAreas []LawyerPracticeArea `gorm:"foreignKey:LawyerID" json:"practice_areas"`
CreatedAt time.Time `gorm:"" json:"created_at"`
UpdatedAt time.Time `gorm:"" json:"updated_at"`
}
这是我的律师实践领域模型:
type LawyerPracticeArea struct {
ID uint `gorm:"primaryKey" json:"lawyer_practice_area_id"`
LawyerID uint `gorm:"not null" json:"lawyer_id"`
PracticeAreaID uint `gorm:"not null" json:"practice_area_id"`
PracticeArea PracticeArea `gorm:"foreignKey:PracticeAreaID" json:"practice_area"`
Charge int `gorm:"" json:"charge"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
最后,这是我的实践领域模型:
type PracticeArea struct {
ID uint `gorm:"primaryKey" json:"practice_area_id"`
Name string `gorm:"not null" json:"name"`
AvgFee string `gorm:"not null" json:"avg_fee"`
}
我通过以下方式查询我的律师模型:
result := db.Preload(clause.Associations).Find(&lawyer)
这个结果包含了所有律师和律师实践领域的数据,但不包含律师实践领域表中的实践领域数据。
律师和实践领域之间是多对多的关系,而律师实践领域是连接它们的表。
如图所示,我收到了一个 practiceAreas 的数组,但没有实践领域的数据。
有没有办法在一次查询中同时查询到这些数据,或者我必须遍历所有的律师,然后遍历每个律师的实践领域,并根据每个实践领域的 ID 查找实践领域的数据?
英文:
Here is my Lawyer Model
type Lawyer struct {
ID uint `gorm:"primaryKey" json:"id"`
FirstName string `gorm:"type:varchar(100) not null" json:"first_name"`
LastName string `gorm:"type:varchar(100) not null" json:"last_name"`
FullName string `gorm:"->;type:text GENERATED ALWAYS AS (concat(first_name,' ',last_name)) VIRTUAL;" json:"full_name"`
LocationID uint `gorm:"not null" json:"location_id"`
Location Location `gorm:"foreignKey:location_id" json:"location"`
Email string `gorm:"unique;not null" json:"email"`
Phone string `gorm:"type:varchar(100);not null" json:"phone"`
Password string `gorm:"type:varchar(100);not null" json:"password"`
ImageURL string `gorm:"type:text" json:"image_url"`
Education string `gorm:"not null" json:"education"`
Experience uint `gorm:"not null" json:"experience"`
PracticeAreas []LawyerPracticeArea `gorm:"foreignKey:LawyerID" json:"practice_areas"`
CreatedAt time.Time `gorm:"" json:"created_at"`
UpdatedAt time.Time `gorm:"" json:"updated_at"`
}
Here is my LawyerPracticeAreas Model
type LawyerPracticeArea struct {
ID uint `gorm:"primaryKey" json:"lawyer_practice_area_id"`
LawyerID uint `gorm:"not null" json:"lawyer_id"`
PracticeAreaID uint `gorm:"not null" json:"practice_area_id"`
PracticeArea PracticeArea `gorm:"foreignKey:PracticeAreaID" json:"practice_area"`
Charge int `gorm:"" json:"charge"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
and last here is my PracticeArea Model
type PracticeArea struct {
ID uint `gorm:"primaryKey" json:"practice_area_id"`
Name string `gorm:"not null" json:"name"`
AvgFee string `gorm:"not null" json:"avg_fee"`
}
I am querying my Lawyer Model through this:-
result := db.Preload(clause.Associations).Find(&lawyer)
This result contains all Lawyers and LawyerPracticeAreas data too, but doesn't contain data from PracticeArea table which is inside LawyerPracticeAreas.
Lawyer and PracticeArea have a many-2-many relationship and LawyerPracticeAreas is that table.
As you can see I am receiving array of practiceAreas but not the data of that PracticeArea.
Is there any way to query that too in just one query or do I have to iterate through all my lawyers then practiceAreas and then for each id find the practiceArea data.
答案1
得分: 2
根据文档:
> clause.Associations
不会预加载嵌套的关联,但你可以与 Nested Preloading 一起使用...
在你的情况下,要加载所有内容,甚至是嵌套多层的关联,你可以这样做:
result := db.Preload("Location").Preload("PracticeAreas.PracticeArea").Find(&lawyer)
英文:
Per documentation:
> clause.Associations
won’t preload nested associations, but you can use it with Nested Preloading together...
In your case, to load everything, even the associations nested more than one level deep, you could do this:
result := db.Preload("Location").Preload("PracticeAreas.PracticeArea").Find(&lawyer)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论