Gorm: Multiple joins are not working properly

Created on 22 Jan 2020  路  3Comments  路  Source: go-gorm/gorm

What version of Go are you using (go version)?

go1.13.6 linux/amd64

Which database and its version are you using?

PostgreSQL 12.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 9.2.0, 64-bit

The problem itself

I have the following struct:

type NoteCard struct {
    Id           int       `json:"id"`
    AuthorId     int       `json:"author_id"`
    AuthorName   string    `json:"author_name"`
    CategoryId   int       `json:"category_id"`
    CategoryName string    `json:"category_name"`
    SubjectId    int       `json:"subject_id"`
    SubjectName  string    `json:"subject_name"`
    TeacherId    int       `json:"teacher_id"`
    TeacherName  string    `json:"teacher_name"`
    PostedAt     time.Time `json:"posted_at"`
    Title        string    `json:"title"`
}

And the following code snippet:

func (api *API) GetNoteCards(c *gin.Context) {
    var noteCards []NoteCard

    searchItems := "notes.id, notes.author_id, users.name, notes.category_id, categories.name, notes.subject_id, subjects.name, " +
        "notes.teacher_id, teachers.name, notes.posted_at, notes.title"

    err := api.db.Table("notes").Select(searchItems).
        Joins("INNER JOIN users ON notes.author_id = users.id").
        Joins("INNER JOIN categories ON notes.category_id = categories.id").
        Joins("INNER JOIN subjects ON notes.subject_id = subjects.id").
        Joins("INNER JOIN teachers ON notes.teacher_id = teachers.id").
        Order("notes.posted_at DESC").
        Scan(&noteCards).Error

    if err != nil {
        log.Println(err)
    }

    if err := json.NewEncoder(c.Writer).Encode(noteCards); err != nil {
        log.Printf("Cannot encode note cards to JSON, error: %v\n", err)
    }
}

This code completes without errors, but the fields of joined tables (users, categories, subjects and teachers) are empty strings. At the same time, when I run this code in psql bash, it works perfectly correctly.

Is this a bug or am I doing something wrong?

gorm_v1

Most helpful comment

Preloads will help you. But generally it is an really epic fail that makes this lib fully unusable. E.g. I need a result sets 100000+ records, but Preloads are just selects with IN STATEMENT-s so there is always a limitation in size. Next obvious workaround is split into parallel chunks but breaks ordering.

All 3 comments

Preloads will help you. But generally it is an really epic fail that makes this lib fully unusable. E.g. I need a result sets 100000+ records, but Preloads are just selects with IN STATEMENT-s so there is always a limitation in size. Next obvious workaround is split into parallel chunks but breaks ordering.

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

littletwolee picture littletwolee  路  3Comments

alanyuen picture alanyuen  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

izouxv picture izouxv  路  3Comments

fieryorc picture fieryorc  路  3Comments