如何在sequelize中重新编写左连接查询?

u5rb5r59  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(204)

我试图编写一个带有左连接的sequelize查询,但结果总是为null。
这是我想在sequelize中重新编写的查询:

`SELECT p.postId, p.postTitle, p.postContent, p.postOnThread, p.postOfAccount, t.threadName 
 FROM post as p 
 LEFT JOIN thread as t 
 ON p.postOnThread = t.threadId 
 WHERE postOnThread = ?
 ORDER by postId DESC`

这是我目前的sequelize查询:

module.exports = function({ SQLiteDb }){
return {
    getAllPosts: function(threadId, callback) {
        SQLiteDb.post.findAll({ 
            where: { threadId: threadId },
            include:[{
                model: SQLiteDb.thread,
                required: false
            }], 
            raw: true 
        })
        .then(posts => console.log(posts)/*callback([], posts)*/)
        .catch(error => callback(['internalError'], null))
    },
}

协会:

db.post.associate = (models) => {
   this.post.belongsTo(models.thread, {
       foreignKey: 'postOnThread'
   })
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题