我對使用續集與'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') 這將工作。括號中使用的表格和列的物理名稱 –