Javascript 续集中的WHERE或WHERE-THROUGH
所以我有3张桌子: 帖子,轴心,类别 在Post表中,有一个主类别字段=main\u category\u idPost一次可以有一个主类别 然后,Pivot表充当次要类别的Pivot,该类别的帖子可以有1个以上 透视图中的字段Javascript 续集中的WHERE或WHERE-THROUGH,javascript,database,postgresql,sequelize.js,Javascript,Database,Postgresql,Sequelize.js,所以我有3张桌子: 帖子,轴心,类别 在Post表中,有一个主类别字段=main\u category\u idPost一次可以有一个主类别 然后,Pivot表充当次要类别的Pivot,该类别的帖子可以有1个以上 透视图中的字段 邮政编码 类别识别码 我正在尝试获取帖子,其中主类别=id或轴心有类别=id 当前代码: var mainPost = await models.t_post.findAll({ where:{ main_category_i
- 邮政编码
- 类别识别码
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
});
我希望有帮助 你能分享Post、Pivot和Category表格的模型吗?我刚刚编辑了模型,还有关联,如果可能的话。嘿!谢谢你的代码帮了我大忙,我不得不稍微修改一下。当前代码:literal(
t_post.id=${parseInt(main.id)}
),literal(secondary_category.id=${parseInt(main.id)}
),但在使用limit时,我从表secondary_category的子句条目中丢失了它,知道它为什么会这样做吗?nvm,我通过在include上添加replication:false
修复了它。非常感谢。