How to get particular fields in Preload while using Gorm

Viewed 27

I have two models which have many to many relations.

type User struct {
    ID        uint      `gorm:"primary_key" json:"id"`
    Name      string    `json:"name"`
    Surname   string    `json:"surname"`
    Email     string    `json:"email"`
    Password  string    `json:"password"`
    RoleID    uint      `json:"role_id"`
    Classes   []*Class  `gorm:"many2many:user_classes;" json:"classes"`
    CreatedAt time.Time `json:"created_at"`
    UpdatedAt time.Time `json:"updated_at"`
}
type Class struct {
    ID        uint       `gorm:"primary_key" json:"id"`
    Name      string     `json:"name"`
    Homeworks []Homework `gorm:"foreignKey:ClassID" json:"homeworks"`
    Materials []Material `gorm:"foreignKey:ClassID" json:"materials"`
    Users     []*User    `gorm:"many2many:classes;" json:"users"`
    CreatedAt time.Time  `json:"created_at"`
    UpdatedAt time.Time  `json:"updated_at"`
}

I want to get users with their classes but with only class id and class name. I did a lot of research and found this solution. However, it didn't work for me. I don't know why. Everything seems fine but didn't work.

My repository function's body is like that

var users []models.User
    err := repo.db.Preload("Classes", func(db *gorm.DB) *gorm.DB {
        return db.Select("ID", "Name")
    }).Find(&users).Error

    return users, err

I am expecting results like that

{
            "id": 4,
            "name": "Furkan",
            "surname": "Topaloğlu",
            "email": "furkantopalogluu@gmail.com",
            "password": "fuki123",
            "role_id": 2,
            "classes": [
                {
                    "id": 1,
                    "name": "IELTS Group 1"
                },
                {
                    "id": 2,
                    "name": "Speaking Club Grup 4"
                }
            ],
            "created_at": "2022-09-20T02:54:07.185756+03:00",
            "updated_at": "2022-09-20T02:54:07.185756+03:00"
}

However, I'm getting this :

{
            "id": 4,
            "name": "Furkan",
            "surname": "Topaloğlu",
            "email": "furkantopalogluu@gmail.com",
            "password": "fuki123",
            "role_id": 2,
            "classes": [
                {
                    "id": 1,
                    "name": "IELTS Group 1",
                    "homeworks": null,
                    "materials": null,
                    "users": null,
                    "created_at": "0001-01-01T00:00:00Z",
                    "updated_at": "0001-01-01T00:00:00Z"
                },
                {
                    "id": 2,
                    "name": "Speaking Club Grup 4",
                    "homeworks": null,
                    "materials": null,
                    "users": null,
                    "created_at": "0001-01-01T00:00:00Z",
                    "updated_at": "0001-01-01T00:00:00Z"
                }
            ],
            "created_at": "2022-09-20T02:54:07.185756+03:00",
            "updated_at": "2022-09-20T02:54:07.185756+03:00"
}

What am I doing wrong? Please help me.

0 Answers
Related