通过Gorm查询模型

发布于 2025-01-22 22:39:16 字数 2624 浏览 0 评论 0原文

这是我的律师模型

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.

[1]: https://i.sstatic.net/XjhB3.png

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情洁癖 2025-01-29 22:39:16

per 文档

条款 不会预紧嵌套的关联,但是您可以将其与嵌套预加载一起使用...

在您的情况下,加载了所有内容,即使是嵌套的关联,您也可以做到,您可以做到这:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文