Home > OS >  Query Models through Gorm
Query Models through Gorm

Time:04-22

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.stack.imgur.com/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.

CodePudding user response:

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)
  • Related