Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correlate BelongTo #6961

Open
pejman-hkh opened this issue Apr 12, 2024 · 12 comments
Open

Correlate BelongTo #6961

pejman-hkh opened this issue Apr 12, 2024 · 12 comments
Assignees
Labels
type:question general questions

Comments

@pejman-hkh
Copy link

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

@pejman-hkh pejman-hkh added the type:question general questions label Apr 12, 2024
@gg1229505432
Copy link

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
Copy link

gg1229505432 commented Apr 14, 2024

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 :
#6943 (comment)

@pejman-hkh
Copy link
Author

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
Copy link

Groups[0] is Groups

@pejman-hkh
Copy link
Author

Let me check it

@pejman-hkh
Copy link
Author

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

@gg1229505432
Copy link

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
Copy link
Author

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

@pejman-hkh pejman-hkh changed the title c BelongTo Correlate BelongTo Apr 15, 2024
@gg1229505432
Copy link

checkout this:
pejman-hkh/gorn#1

@pejman-hkh
Copy link
Author

pejman-hkh commented Apr 17, 2024

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
Copy link
Author

pejman-hkh commented Apr 18, 2024

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
Copy link

@jinzhu you can close this issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

3 participants