A better way to query relations from ids of the other? #327
Answered
by
vmihailenco
frederikhors
asked this question in
Q&A
-
May I ask you what would you do in this case? I have this situation: type Profile struct {
ID int64 `bun:",pk"`
Lang string
UserID int64
User *User `rel:"belongs-to"`
}
type User struct {
ID int64 `bun:",pk"`
Name string
Profile *Profile `bun:"rel:has-one"`
} I'm using GraphQL and in a resolver I need to retrieve all Users from Profiles IDs. Right now I'm doing this: func (r repo) UsersByProfileIDs(ctx context.Context, ids []uint64) ([]User, error) {
res := make([]User, 0, len(ids))
err := r.db.NewSelect().Model(&res).Relation("Profile").Where("profile.user_id in (?)", bun.In(ids)).Scan(ctx)
if err != nil {
// handle err
}
return res, nil
} This works but I was wondering if it's a choice you appreciate or if I can do better. |
Beta Was this translation helpful? Give feedback.
Answered by
vmihailenco
Nov 29, 2021
Replies: 1 comment
-
Looks pretty good 👍 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
frederikhors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks pretty good 👍