我对使用续集与'include'有点问题。 问题是我的模型在子表中使用了两个主键。续集包含多个条件的情况下
所以它是这样的 父表 用户:ID,...
帖子:ID,用户ID(外键,结合用户ID),... 后散列标签:#标签,帖子ID(国外关键,结合文章ID),用户ID(外键,结合邮政表的用户ID)
所以表层次结构看起来像这样 用户 - 后 - 后散列标签现在
当我尝试做像这样,
Post.findAll(
include: {
model: post hash tag
}
)
那么它只能搜索其中后散列标签表的帖子的ID等于张贴
所以我说像这样
Post.findAll(
include: {
model: post hash tag
where: {
col1: models.sequelize.where(models.sequelize.col('POST.USER_ID'), '=', models.sequelize.col('POST_HASH_TAG.USER_ID'))
}
}
)
然后交表的ID它会给出了一个问题后哈希标签在'where'子句中找不到Post.USER_ID。
如果我改变COL1值Post.userId那么现在它解决了上面的错误,但在给出了另一个错误“上”子句
你有什么想法,我怎么能解决这个问题?
完整的模型这里给出
用户 sequelize.define( '用户',{ ID:{类型:DataTypes.STRING(6),场: 'ID',的PrimaryKey:真} )
后 - 我知道多原申报工作不正常,所以不要刻意去考虑太多
sequelize.define('Post', {
id: { type: DataTypes.STRING(6), field: 'ID', primaryKey: true },
userId: { type: DataTypes.STRING(6), field: 'USER_ID', primaryKey: true }
)
后散列标签
sequelize.define('PostHashTag', {
postId: { type: DataTypes.STRING(6), field: 'POST_ID', primaryKey: true },
hashTag: { type: DataTypes.STRING(20), field: 'HASH_TAG', primaryKey: true },
userId: { type: DataTypes.STRING(6), field: 'USER_ID', primaryKey: true }
}
)
和我使用的查询是
Post.findAll({
attributes: ['id', 'userId'],
where: {
userId: userId,
id: { $lt: postId }
},
include: [{
model: models.PostHashTag,
attributes: ['hashTag'],
where: {
col1: models.sequelize.where(models.sequelize.col('Post.USER_ID'), '=', models.sequelize.col('PostHashTag.userId'))
}]).then(...)
我自己找到了答案... col1:models.sequelize.where('Post.USER_ID'),'=',models。 sequelize.col('PostHashTag.userId')) 这应该是 userId:models.sequelize.col('POST_HASH_TAG.USER_ID') 这将工作。括号中使用的表格和列的物理名称 –