Search code examples
strapibookshelf.js

How to select columns from a relation


so I'm using Strapi and Bookshelf. The .query() is a normal bookshelf query.

const result = await strapi.query('friendship').model
    .query({
        where: { user1: 1 },
        orWhere: { user2: 1 }
    })
    .fetchAll()

Now, user1,2 are 1-to-1 relations to user and I'd like to select e.g. id and username. If I output result, everythings there. How can I tell it to just select those columns?


Solution

  • The following (specifying columns in fetchAll()) works for me however there's no relations in my case:

    const result = await strapi.query('friendship').model
      .query({
        where: { user1: 1 },
        orWhere: { user2: 1 }
      })
      .fetchAll({columns:["id", "username"]})
    

    You can refer to the options description from Bookshelf.js.