javascript - 在 Sequelize 中的哪里或哪里通过

标签 javascript database postgresql sequelize.js

所以我有 3 张 table :
发布 , 枢轴 , 类别

Post 表中有一个字段用于主类别 = main_category_id 帖子一次可以有一个主类别。

然后 数据透视表 表用作次要类别的数据透视表,帖子可以有 1 个以上。

中的字段 Pivot

  • post_id
  • category_id

  • 我正在尝试获取 Post ,其中 main_category = id 或枢轴具有 category_id = id

    当前代码:
    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,
        })
    

    编辑:添加模型

    岗位模型
    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;
    };
    

    枢轴模型
    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;
    };
    

    类别模型
    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;
    };
    

    协会
    //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"
    })
    

    最佳答案

    请引用以下代码以您想要的方式过滤记录 -

      const 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']
          }
        ],
        where: {
          [op.or]: [
            db.sequelize.literal(`\`t_post\`.\`main_category_id\` = ${main.id}`),
            db.sequelize.literal(`\`m_category\`.\`category_id\` = ${main.id}`)
          ]
        },
        order: [['createdAt', 'desc']],
        limit: limit,
        offset: 3 + limit * page - limit
      });
    

    我希望它有帮助!

    关于javascript - 在 Sequelize 中的哪里或哪里通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60406658/

    相关文章:

    mysql - 我该如何解决这个 System.IndexOutOfRangeException : Index was outside the bounds of the array

    python - 从 Django 的 PostgreSQL 的 ArrayField 中删除元素

    javascript - 涉及继承时存储 ES6 Javascript 类 getter/setter

    javascript - jQuery 的更改方法在这里做什么(如果有的话)?

    sql - Postgres 更新规则返回受影响的行数

    c# - 从头开始使用 NHibernate : tips for a new, 大型应用程序

    sql - "NOT IN"Postgres 的正确使用方法

    sql - 在给定日期之间获取最高分条目

    javascript - 在 FireFox 中调整容器大小时(通过 javascript - jQuery),我的闪存重新初始化(重新加载)

    显示站点 URL 的 Javascript 警报。如何从 javascript 警报顶部删除站点 url?