通过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)
此结果也包含所有律师和律师事务所的数据,但不包含从实践中的数据,该数据是内部律师事务所的律师事务所的律师。 。
律师和实践中的律师有很多2次的关系,而律师是那张桌子。
,您可以看到我正在收到一系列实践中的阵列,但没有收到该实践中的数据。
是否有任何方法可以在一个查询中查询,还是我必须迭代所有律师,然后再仔细阅读练习,然后为每个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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
per 文档:
在您的情况下,加载了所有内容,即使是嵌套的关联,您也可以做到,您可以做到这:
Per documentation:
In your case, to load everything, even the associations nested more than one level deep, you could do this: