Luicd Method Pick(x=rows) not working as expected

I am trying to fetch posts with only 2 comments , for that i tried to use pick method which does not work, how do i acheive fetching x rows only with .with() clause?
please help.

My code :

const posts = await PostMaster.query()

    .active()

    .where("campus_id", campus_id)

    .with("postDetail", (builder) =>

      builder.select("post_id", "post_detail_title", "image_url")

    )

    .with("comments.user", (builder) =>

      builder.select(

        "id",

        "first_name",

        "last_name",

        "email",

        "profile_pic_url"

      ).pick(2)

    )

    .withCount('comments')

    .with("users", (builder) =>

      builder.select(

        "id",

        "first_name",

        "last_name",

        "email",

        "profile_pic_url"

      )

    )

    .with("userFollowing", (builder) => builder.where("follower_id", auth.user.id))

    .with("userSavedPost", (builder) =>

      builder.where("user_id", auth.user.id)

    )

    .with("userWiseLike", (builder) =>

      builder.where("user_id", auth.user.id)

    )

    .with("postCategory")

    .with("campuses", (builder) => builder.select("id", "description"))

    .fetch()

OUPUT : … , comments[ ]
all comments [ ] empty except for first result.

it worked fine when i tried to fetch only single post with where("post_id, 10) ,
but with many posts it does not work. please help

1 Like

Pick is not working, because it calls the fetch method, just use limit instead of it

builder.limit(2)
2 Likes

Thanks @keef3ar , i just tried using limit instead of pick , and it still gives the same output i.e empty array for comments

can u call debug() from builder or before fetch(i don’t remember how it implements), it will show u debug info with sql query.

If u remove limit and pick either, it gives u an expected array?

If i remove limit and pick both , i gives me array with all the comments in it , i just need 2

I have tested it.
I thing here is some trouble to specify fields(select)
This code is works fine

 const flow_test = await Flow.query().where({id: 6}).with('clicks', builder => {
      builder.limit(2)
    }).first()
    const clicks = flow_test.getRelated('clicks')
    console.log(clicks.rows.length)

try fetching multiple rows , Flow.query().where({active:1}) etc
and as you can see in my code , i have builder inside a builder .


U’re right, in this case .limit() works unexpected, with parent builder, probably bug in lucid ORM

1 Like

I think it is need to specify limit target, like i do in

.where("transactions.created_at", "<", to_date)

without specify a table it works with parent builder to.
But i have no idea and time to find how to specify table for limiting in nested builder.
If u’ll find a solution, please notify me

1 Like