4

I have a model Category with a belongs to many relationship with itself:

  • Category has many categories in subcategories
  • Category belongs to one category in supercategory

When I query all categories I get the correct results:

  • strapi.services.category.find()

When I query all subcategories I also get the correct results:

  • strapi.services.category.find({ supercategory_null: false })

But when I query just supercategories it doesn't return any categories:

  • strapi.services.category.find({ supercategory_null: true })

Question

How can I query just the categories that have no supercategory relationship?

3 Answers 3

0

This should work

strapi.services.category.find({ supercategory_null: true },['category']);

Defination: find(params, populate)

populate (array): you have to mention data you want populate e.g ["author", "author.name", "comment", "comment.content"]

Sign up to request clarification or add additional context in comments.

Comments

0

The solution I found is to use knex instead of the strapi api:

// Assuming your table name is `category`
const knex = strapi.connections.default;
const categories = await knex(`category`).select(`*`).where(`supercategory`, null);

Comments

0

How can I query just the categories that have no supercategory relationship?

Using entityService maybe:

const res = await strapi.entityService.findMany('api::category.category', {
  filters: {
    supercategory: {
      $null: true,
    },
  },
  populate: { supercategory: true },
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.