bun icon indicating copy to clipboard operation
bun copied to clipboard

Limit does not work properly with m2m

Open imraan-go opened this issue 4 years ago • 1 comments

So here is the scenario, I want to select 2 items from each order on the following example: https://bun.uptrace.dev/guide/relations.html#many-to-many-relation

orders := []Order{}
db.NewSelect().Model(&orders).Relation("Items", func(q *bun.SelectQuery) *bun.SelectQuery {
		return q.Limit(2)
	}).Scan(ctx)

The following query only selects first 2 items instead of selecting 2 items per product.

imraan-go avatar Mar 23 '22 09:03 imraan-go

This is a limitation of the current m2m query generator. Perhaps we should check if limit != 0 and issue a separate query for each order.

vmihailenco avatar Mar 23 '22 13:03 vmihailenco