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

Help me to get the relation. #1978

Open
jnsand opened this issue Mar 24, 2023 Discussed in #1977 · 0 comments
Open

Help me to get the relation. #1978

jnsand opened this issue Mar 24, 2023 Discussed in #1977 · 0 comments

Comments

@jnsand
Copy link

jnsand commented Mar 24, 2023

Discussed in #1977

Originally posted by Sirius198 March 23, 2023

type Friend struct {
	tableName struct{}  `pg: tbl_friends,alias:tf"`
	Id        uuid.UUID `pg:"id, type:uuid"`
	AccountId uuid.UUID `pg:"account_id,type:uuid"`
	FriendId  uuid.UUID `pg:"friend_id,type:uuid"`
	Profile   *Profile  `pg:"rel:has-one, fk:friend_id, join_fk:account_id"`
}

type Profile struct {
	tableName         struct{}  `pg:"tbl_profiles,alias:tp"`
	Id                uuid.UUID `pg:"id,type:uuid"`
	AccountId         uuid.UUID `pg:"account_id,type:uuid"`
	PlayerCode        string    `pg:"player_code"`
	PlayerName        string    `pg:"player_name"`
}

var friends []models.Friend
var accountID uuid.UUID
db.Model(&friends).Relation("Profile").Where("tf.account_id = ?", accountID).Select()

After executing this query, friends.Profile is always nil.

My query result is same with below SQL query?

SELECT * FROM tbl_friends tf LEFT JOIN tbl_profiles tp ON tf.friend_id = tp.account_id WHERE tf.account_id = '1111-1111-1111-'

Thanks.

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

No branches or pull requests

1 participant