uptrace / bun

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

Limit does not work properly with m2m #496

Open imraan-go opened 2 years ago

imraan-go commented 2 years ago

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.

vmihailenco commented 2 years ago

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.