J
J
jenya77712018-12-09 22:17:41
ORM
jenya7771, 2018-12-09 22:17:41

How to search by child categories in sequelize db?

Hello, there are tables of news, categories and a table of categories with news.

const news = sequelize.define('news', {
  name:  { 
    type: Sequelize.STRING, 
    allowNull: false
    }
})

const categories = sequelize.define('categories', {
  name:  { 
    type: Sequelize.STRING, 
    allowNull: false,
  },
  parental_id: { 
    type: Sequelize.INTEGER, 
    defaultValue: null,
    foreignKey : true
  }
})

const categoriesNews = sequelize.define('news_categories', {})
news.belongsToMany(categories, {through: categoriesNews })
categories.belongsToMany(news, {through: categoriesNews })
categories.belongsTo(categories, {foreignKey : 'parental_id', sourceKey: 'id', as: 'categoriesParental'})

How can I get news not only by category, but also by its child categories?
For example, there are categories:
Recreation
-Sports -Football
And
if I search for news in the category Recreation, how can I get news and not only in the Recreation category itself, but also in child categories?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question