How do I return only selected fields from the model and its relation

Thanks for the detailed response.
I could somehow arrive at this solution with little modification as per my database schema relations.

final working query for me is .

  find: async (ctx) => {
        const result = await strapi.query("subcategory").model.fetchAll({
          columns: ["id", "name","basecategory"],
          withRelated: [
            {
              'basecategory': qb => {qb.columns("id","name")},
              'image': qb => {qb.columns('id','url')},
            },
          ],
        });
        ctx.send(result)
     },

The only issue i left with is, populating the image relation.

subcategory

  • id
  • name
  • desc
  • image->has_one_relation
  • basecategory->has_one_relation

This is the current output response i am getting

[
  {
    "id": 1,
    "name": "esso1",
    "basecategory": {
      "id": 1,
      "name": "Esso"
    },
    "image": {
      "id": 6,
      "name": "esso.png",
      "alternativeText": "",
      "caption": "",
      "width": 416,
      "height": 260,
      "formats": {
        "thumbnail": {
          "ext": ".png",
          "url": "/uploads/thumbnail_esso_6e3f8e59fb.png",
          "hash": "thumbnail_esso_6e3f8e59fb",
          "mime": "image/png",
          "name": "thumbnail_esso.png",
          "path": null,
          "size": 60.33,
          "width": 245,
          "height": 153
        }
      },
      "hash": "esso_6e3f8e59fb",
      "ext": ".png",
      "mime": "image/png",
      "size": 138.21,
      "url": "/uploads/esso_6e3f8e59fb.png",
      "previewUrl": null,
      "provider": "local",
      "provider_metadata": null,
      "created_by": 1,
      "updated_by": 1,
      "created_at": "2020-11-08T13:25:35.286Z",
      "updated_at": "2020-11-08T13:25:35.304Z"
    }
  }
]

i tried all the variations to get only the image url field but i coudn’t make it.