Answer the question
In order to leave comments, you need to log in
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'})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question