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

xxxxJoinOnField func joins both tables with the same field name,Not widely applicable #2675

Open
kg1990 opened this issue May 30, 2023 · 3 comments
Labels
feature help wanted planned This issue/proposal is planned into our next steps.

Comments

@kg1990
Copy link

kg1990 commented May 30, 2023

目前框架里面的JoinOnField的函数只支持相同列名的join,这个在实际的数据库设计场景里面所覆盖度不是很广

例如:

A表主键id字段,B表依赖于A表的id字段,设置字段名为a_id,在下面的示例里面,例如user,一般不会设置一个user_id的字段作为自己的主键

虽然有直接的leftjoin的方法支持,但是是属于硬编码支持,推荐在里面支持一下源字段和连接的目标字段,这样应该更加通用一点

// LeftJoinOnField performs as LeftJoin, but it joins both tables with the same field name.
//
// Eg:
// Model("order").LeftJoinOnField("user", "user_id")
// Model("order").LeftJoinOnField("product", "product_id").
func (m *Model) LeftJoinOnField(table, field string) *Model {
	return m.doJoin("LEFT", table, fmt.Sprintf(
		`%s.%s=%s.%s`,
		m.tablesInit,
		m.db.GetCore().QuoteWord(field),
		m.db.GetCore().QuoteWord(table),
		m.db.GetCore().QuoteWord(field),
	))
}
// LeftJoinOnField performs as LeftJoin.
//
// Eg:
// Model("order").LeftJoinOnField("user_id", "user", "id")
// Model("order").LeftJoinOnField("product_id", "product", "id").
func (m *Model) LeftJoinOnField(fieldA string, table, fieldB string) *Model {
	return m.doJoin("LEFT", table, fmt.Sprintf(
		`%s.%s=%s.%s`,
		m.tablesInit,
		m.db.GetCore().QuoteWord(fieldA),
		m.db.GetCore().QuoteWord(table),
		m.db.GetCore().QuoteWord(fieldB),
	))
}
@kg1990 kg1990 changed the title Not widely applicable xxxxJoinOnField func joins both tables with the same field name,Not widely applicable May 30, 2023
@gqcn
Copy link
Member

gqcn commented Aug 22, 2023

@kg1990 这个建议不错!考虑到兼容性,这里可以新增加一个方法来实现,例如LeftJoinOnFields

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


Good suggestion @kg1990! Considering compatibility, a new method can be added here, such as LeftJoinOnFields

@gqcn gqcn added feature help wanted planned This issue/proposal is planned into our next steps. labels Aug 22, 2023
@github-actions
Copy link

Hello @kg1990. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it!
你好 @kg1990。我们喜欢您的提案/反馈,并希望您或其他社区成员通过拉取请求做出贡献。我们提前感谢您的贡献,并期待对其进行审查。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature help wanted planned This issue/proposal is planned into our next steps.
Projects
None yet
Development

No branches or pull requests

3 participants