go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.95k stars 3.93k forks source link

Correlate BelongTo #6961

Open pejman-hkh opened 7 months ago

pejman-hkh commented 7 months ago

Your Question

Consider we have a Group model, and Group model has UserId and User model has GroupId and UserId too. How can I set User Belong to Group ?

type User struct {
    BaseModel
    Name            string `gorm:"size:255"`
    Email           string `gorm:"type:varchar(100);index:idx_email"`
    Password        string `gorm:"size:255" json:"-"`
    EmailVerifiedAt time.Time
    IsAdmin         bool
    IsMain          bool
    UserID          uint `gorm:"index"`
    GroupID         uint `gorm:"index"`
    Group           Group
}
type Group struct {
    BaseModel
    Title  string `gorm:"size:255"`
    UserId uint   `gorm:"index"`
    User   User   `json:"user"`
}

I go below error in golang compilation: invalid recursive type Group

The document you expected this should be explained

Expected answer

gg1229505432 commented 7 months ago

Your current error is not a bug with the gorm framework, but a basic program compilation error. When the object is initialized, it will cause a cross-pointer reference, but the referenced object is not fully initialized, so it will cause infinite recursion.

gg1229505432 commented 7 months ago

If you want to correlate the two, consider referencing the array of the other struct in the current struct type User struct { BaseModel UserID uint gorm:"index" GroupID uint gorm:"index" Groups []Group json:"groups" }

type Group struct { BaseModel UserId uint gorm:"index" Users []User json:"users" }

The sample you can refer to : https://github.com/go-gorm/gorm/issues/6943#issuecomment-2053661459

pejman-hkh commented 7 months ago

I know that is compilation error. in my case User model is belong to Group and Group model is belong to User and I don't have array and your answer is not the solution.

gg1229505432 commented 7 months ago

Groups[0] is Groups

pejman-hkh commented 7 months ago

Let me check it

pejman-hkh commented 7 months ago

It returns empty array I also set preload Preload("Users")

gg1229505432 commented 7 months ago

please give me code sample, I'm at work right now and don't have much time to solve your problem, I will response you later 😅

pejman-hkh commented 7 months ago

thank you a lot for your response and happy for you that have a job :D my project link:

https://github.com/pejman-hkh/gorn/blob/main/api/app/model/group.go

gg1229505432 commented 7 months ago

checkout this: https://github.com/pejman-hkh/gorn/pull/1

pejman-hkh commented 7 months ago

Let me describe better my problem. My group has a user creator that created it or I have a User that another user created it, How can get this User in belong to and preload. I don't need users of one group ! I just want user that is belong to userid column on same table.

pejman-hkh commented 7 months ago

I solved this problem with made another package name with same model name with required fields. with helper model I can fetch each fields that I require.

If Gorm let us to define Table Name in struct we can fetch each fields we require with define struct on same place :

type Group struct {
    Title       string       `gorm:"size:255" json:"title"`
    UserId      uint         `gorm:"index" json:"user_id"`
    User struct {
        Table string `gorm:"table:users"`
        ID        uint
        Name      string
    }
}
gg1229505432 commented 7 months ago

@jinzhu you can close this issues