bun icon indicating copy to clipboard operation
bun copied to clipboard

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

Open nelsonnba opened this issue 1 year ago • 0 comments

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")
		})

nelsonnba avatar Sep 27 '23 20:09 nelsonnba