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

Allow for inner or right join in Join preloading #4872

Open
williamfinn opened this issue Nov 23, 2021 · 10 comments
Open

Allow for inner or right join in Join preloading #4872

williamfinn opened this issue Nov 23, 2021 · 10 comments
Assignees
Labels
type:feature_request feature request

Comments

@williamfinn
Copy link

Describe the feature

Would like to be able to specify a Join type / direction when using Joins Preloading.

Motivation

Currently the documentation says that an "inner" join is carried out but in fact a left join is carried out. I think that it would be an excellent feature to have other join types allowed for join preloading for example: right joining between multiple entities will only return a row if all of the right joins are satisfied.

Code example

type Card struct {
	Id           int          `json:"id,omitempty" gorm:"column:id"`
	Amount       float64      `json:"amount,omitempty" gorm:"column:amount"`
	Currency     string       `json:"currency,omitempty" gorm:"column:currency"`
	GiftCard     Giftcard     `json:"giftCard,omitempty" gorm:"foreignKey:CardId"`
	CardConsumer CardConsumer `json:"consumer,omitempty" gorm:"foreignKey:CardId"`
}

// note that the entity name is specified "GiftCard" and "CardConsumer" to facilitate join preloading
r.db.Joins("right join GiftCard").Joins("right join CardConsumer").Find(&card)
@github-actions
Copy link

This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days

@mnussbaum
Copy link
Contributor

This is indeed a discrepancy between the docs and the code, and the ability to do inner joins would be very helpful

@shcw
Copy link

shcw commented Aug 3, 2022

@jinzhu 大佬 这个问题还挺有帮助的 想要使用关联关系的时候 right join 怎么操作呀

I also had the same problem

@williamfinn
Copy link
Author

williamfinn commented Aug 6, 2022

On second thought, doesn't make much sense for right join preloading (as you could have no parent object to preload on). If you want to simulate inner or right joins currently I suggest just doing this in where conditions (CardConsumer.id != null for example). Ability to do inner join would be a useful feature though

@williamfinn williamfinn changed the title Allow for right join in Join preloading Allow for inner or right join in Join preloading Aug 6, 2022
@tbistr
Copy link

tbistr commented Aug 24, 2022

Why does gorm replace "JOIN" with "LEFT JOIN"?
Just "JOIN" means "INNER JOIN" in MySQL and PostgressSQL at least.

I had the same problem.

minimam sample

type A struct {
	ID uint
	B  B
}

type B struct {
	ID   uint
	AID  uint
	Data string
}

db.Create(&A{B: B{Data: "data1"},})
db.Create(&A{B: B{Data: "data2"},})
db.Create(&A{B: B{Data: "data3"},})
As := []A{}
db.Debug().Joins("B", db.Where(&B{Data: "data3"})).Find(&As)
fmt.Println(As)

This code execute SQL like bellow.

SELECT `as`.`id`,`B`.`id` AS `B__id`,`B`.`a_id` AS `B__a_id`,`B`.`data` AS `B__data` FROM `as` LEFT JOIN `bs` `B` ON `as`.`id` = `B`.`a_id` AND `B`.`data` = 'data3'

Then returns [{1 {0 0 }} {2 {0 0 }} {3 {3 3 data3}}].
And just remove "LEFT" solves this problem, returns [{3 {3 3 data3}].

I think the default should be "JOIN" instead "LEFT JOIN".
Are there any issues that I'm not taking into account?

@ChaminW
Copy link

ChaminW commented Sep 22, 2022

As @tbistr mentioned, shouldn't it be "JOIN" as the default instead "LEFT JOIN" for .Join()?

@shcw
Copy link

shcw commented Sep 22, 2022

On second thought, doesn't make much sense for right join preloading (as you could have no parent object to preload on). If you want to simulate inner or right joins currently I suggest just doing this in where conditions (CardConsumer.id != null for example). Ability to do inner join would be a useful feature though

but it still feels useful 😂

@shashankkumarIITB
Copy link

Still facing the same issue.

@delvatt
Copy link

delvatt commented Nov 16, 2022

Facing the same issue. we definitely need more clarity on this, either by specifying in documentation that all JOINS are in fact LEFT JOINS or by implementing the ability to specify the JOIN nature/direction.

@OskarsPakers
Copy link

Inner joins are supported with .InnerJoins method https://github.com/go-gorm/gorm/pull/5583/files
I have submitted PR to add it to documentation go-gorm/gorm.io#635

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

No branches or pull requests

9 participants