uptrace / bun

SQL-first Golang ORM
https://bun.uptrace.dev
BSD 2-Clause "Simplified" License
3.65k stars 221 forks source link

There is not way to perform an inner join for a many relation #909

Open nelsonnba opened 1 year ago

nelsonnba commented 1 year ago

having a model relation

type TableA struct {
   ID int
   Name string
   TableBs []*TableB  `bun:"rel:has-many,join:id=table_aid"`
 }
type TableB struct{
  ID int
 TableAID int
  Num int
}

There is no way to perform an inner join for a many relation using ORM Relation, in order to exclude TableA elements without TableB elements that suit one condition.

The following query gets TablaA elements with nil TableBs field, and there is no way to exclude them:

db.NewSelect().Model(tablaA).Relation("TableBs", func(q *bun.SelectQuery) *bun.SelectQuery {
            return q.Where("num = 1")
        })