-
-
Notifications
You must be signed in to change notification settings - Fork 233
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
How to add custom where clause on Relation? #554
Comments
How |
If we will implement this we will break current behaviour. @vmihailenco is it ok? |
I think we should make this work for has-one and belongs-to relations: q.Relation("Stock", func(q *bun.SelectQuery) *bun.SelectQuery {
return q.JoinOn("branch_id = ?", filter.BranchId)
}) |
I can't seem to make q.JoinOn("branch_id = ?", filter.BranchId) work. Do you have any example that will additional conditional Join statemment? I've tried custom join and joinOn combination like this q.Join("LEFT JOIN stocks AS stock").
JoinOn("stock.id = product.id").
JoinOn("stock.branch_id = ?", filter.BranchId) But this does not select stock columns at all. Is this a bug? |
@imraan-go Did you ever figure this out? I'm having the exact same problem, tried it with |
same situation here. Two queries with different WHERE positions have different meanings. Furthermore, IMO, In the current behavior of Bun, I think it is better to use |
Same situation for me. I need to add a join on clause with Relation (in order to use bun auto populate), but it seems like it doesn't even respect what I have put in the JoinOn clause. |
@vmihailenco any updates on implementing this? I tried to also use raw sql but that time I wasnt able to map joined tables to their own structs. |
|
As a quick solution, I made for my needs it's changing from type Cluster struct {
Servers []*Server `bun:"rel:has-many,join:id=cluster_id"`
} Then I added var entries []*models.Cluster
query := p.db.NewSelect().Model(&entries)
query.Relation("Servers", func(q *bun.SelectQuery) *bun.SelectQuery {
q.Where("server.workspace_id=?", *f.WorkspaceID) // filter
return q
}) So I still see all clusters (main entity) and optionally I can see an installation of that cluster (server) but only if this installation is within the current WorkspaceID Hope this will help someone. Cheers. |
you should probably first add "join:id=blabla" to bun field |
I actually thought about that too but the problem with that is I want to use sql functions and primitivies like "some_date > now()" or "active=false". bun straight throws errors when I try to |
2 years later and the problem still exists. The current behavior doesn't make sense. If I want the Where in the base query, I will add it to the base query. The code suggests applying conditions on the join. I would settle for the solution of vmihailenco the only workaround now is to select all the columns you want to populate structs in a ColumnExpr()
|
This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed. |
Is there any way to add additional condition when using Relation ?
This above code should produce something like:
The Where condition should be inside the left join clause. But currently it produces something like:
Therefore left join does not work. I can do the same with
Join()
andJoinOn()
but then Bun does not populate struct for some reason.The text was updated successfully, but these errors were encountered: