Lucid selects all fields and ignores .select()

If I run this query

 var result = await purchaseOrder
    .query().
    select("PurchaseOrder", "InvoiceFromBusinessPartner")
    .with("purchaseOrderLine", (builder)=>{
      builder.select("PurchaseOrder", "Position", "Item")
    })
    .orderBy("PurchaseOrder", "ASC")
    .where('PurchaseOrder', '127000322')
    .fetch()

I get the fields, you would expect
PurchaseOrder: (“PurchaseOrder”, “InvoiceFromBuinsessPartner”)
PurchaseOrderLine: (“PurchaseOrder”, “Position”, “Item”)

But If run this query, which selects from a level below PurchaseOrderLine in table item, all fields are selected from the item table, when I only selected (“Item”, “Description”)

var result = await purchaseOrder
    .query().
    select("PurchaseOrder", "InvoiceFromBusinessPartner")
    .with("purchaseOrderLine", (builder)=>{
      builder.select("PurchaseOrder", "Position", "Item")
    })
    .orderBy("PurchaseOrder", "ASC")
    .where('PurchaseOrder', '127000322')
    .with("purchaseOrderLine.item", (builder)=>{
      builder.select("Item", "Description")
    })
    .fetch()
1 Like

Hi @virk , is this anything that you could shed any light on?

Interesting issue. Either this is a bug or something is missing.

Not sure if this will be useful, but I think when we use that (builder) notation, the natural way AdonisJs reacts is that it expects to execute a runtime condition.

So maybe you can explore this door: just try to apply a random -but coherent- runtime condition followed by a .select("Item", "Description") to filter the result … and see what happens.

1 Like

I had a play around and found the following format works. For some reason I thought I had read that you can not nest with statements, maybe I’m wrong or maybe there has been some update to Adonis that lifted that restriction.

var result = await purchaseOrder
    .query()
    .where('PurchaseOrder', '127001822')
    .select("PurchaseOrder")
    .with('purchaseOrderLine', (builder)=>{
      builder.where("Position", 94).select("Position", "PurchaseOrder", "Item")
      .with("item", (builder)=>{
        builder.select("Item", "Description")
      })
    })
    .fetch()
    return result.toJSON()
2 Likes