Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140190

WHERE OR WHERE THROUGH in Sequelize

$
0
0

so i have 3 tables : Post , Pivot , Category

in the Post table there is a field for main category = main_category_id post can have one Main Category at a time.

and then the Pivot table serve as pivot for Secondary Category which the post can have more than 1.

fields in Pivot

  • post_id
  • category_id

I'm trying to get the Post where the main_category = id or where the pivot have category_id = id

Current code :

var mainPost = await models.t_post.findAll({
        where:{
            main_category_id:main.id
        },
        include: [{
            model: models.m_category,
            as:"secondary_category",
            through: { 
                where: {category_id: main.id},
            },
            attributes: ['id']
        }],
        order:[
            ['createdAt',"desc"]
        ],
        limit:limit,
        offset:3+(limit*page)-limit,
    })

EDIT : Added models

Post Model

module.exports = (sequelize, DataTypes) => {
  const T_Post = sequelize.define('t_post', {
    user_id: DataTypes.INTEGER,
    title: DataTypes.STRING,
    subtitle: DataTypes.STRING,
    content: DataTypes.TEXT,
    excerpt: DataTypes.TEXT,
    thumbnail: DataTypes.STRING,
    thumbnail_caption: DataTypes.STRING,
    slug: DataTypes.STRING,
    permalink: DataTypes.STRING,
    template: DataTypes.INTEGER,
    published: DataTypes.BOOLEAN,
    published_date: DataTypes.DATE, 
  }, {
    timestamps: true,
    paranoid:true,
    underscored: true,
    freezeTableName: true,
    tableName: 't_post',
    paranoid: true,
  });
  return T_Post;
};

Pivot Model

module.exports = (sequelize, DataTypes) => {
  const m_post_category = sequelize.define('m_post_category', {
    category_id: DataTypes.INTEGER,
    post_id: DataTypes.INTEGER,
  }, {
    timestamps: true,
    paranoid:true,
    underscored: true,
    freezeTableName: true,
    tableName: 'm_post_category'
  });
  return m_post_category;
};

Category Model

module.exports = (sequelize, DataTypes) => {
  const m_category = sequelize.define('m_category', {
    name: DataTypes.STRING,
    slug: DataTypes.STRING,
    template_id: DataTypes.STRING,
    is_active: DataTypes.BOOLEAN,
    has_title: DataTypes.BOOLEAN,
    has_sub_menu: DataTypes.BOOLEAN,
  }, {
    timestamps: true,
    paranoid:true,
    underscored: true,
    freezeTableName: true,
    tableName: 'm_category'
  });
  return m_category;
};

Association

//Get Main category in this post
t_post.belongsTo(m_category,{
  foreignKey:"main_category_id",
  as:"main_category"
})

//Get secondary category in this post
t_post.belongsToMany(m_category,{
  through: m_post_category,
  as: 'secondary_category',
  foreignKey: 'post_id'
})

//Get post that have this category as secondary category
m_category.belongsToMany(t_post,{
  through: m_post_category,
  as: 'article_as_secondary',
  foreignKey: 'category_id'
})

//Eager Loading get post with this category **category.posts**
m_category.hasMany(t_post,{
  as:"posts",
  foreignKey:"main_category_id"
})

Viewing all articles
Browse latest Browse all 140190

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>