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

ColumnExpr with Relation queries #155

Closed
YeemBoi opened this issue Sep 4, 2021 · 2 comments
Closed

ColumnExpr with Relation queries #155

YeemBoi opened this issue Sep 4, 2021 · 2 comments

Comments

@YeemBoi
Copy link

YeemBoi commented Sep 4, 2021

Hi,
I've been looking for functionality similar to ColumnExpr() that will select into the right field when used in a Relation query.
Although it's a different module, go-pg's Issue #1852 describes this exact problem.

This could, perhaps, be addressed be implementing a function similar to *InsertQuery.Value(column string, expr string, args ...interface{}), so that the name of the column could be taken as a separate argument. Then, maybe it could be handled similar to the way #139 addressed this?

See this gist for a clearer example.
Any help would be much appreciated.

@vmihailenco
Copy link
Member

Can you try:

func AlsoAnInvalidRelationQuery(q *bun.SelectQuery) *bun.SelectQuery {
	return q.ColumnExpr("? AS author__name", "Ronald")
}

@YeemBoi
Copy link
Author

YeemBoi commented Sep 5, 2021

Sorry for your time, the issue was that the Relation() apply func was being used in different joins (with different prefixes / aliases)...

The solution is to pass the prefix / alias (eg "author__") to a parent func

func GetRelationQuery(joinPrefix string) func(*bun.SelectQuery) *bun.SelectQuery {
	return func(q *bun.SelectQuery) *bun.SelectQuery {
		return q.ColumnExpr("? AS ?", "Ronald", bun.Ident(joinPrefix+"name"))
	}
}

@YeemBoi YeemBoi closed this as completed Sep 5, 2021
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

2 participants