如何在 sequelize 中正确关联表?

How to correctly associate tables in sequelize?

我正在尝试将关联添加到我定义的表中,但我不确定是否正确完成了。基本上我有一个 MySQL 数据库,我想在 sequelize 中重新创建它作为第二个数据库。

MySQL 个表格:

CREATE TABLE IF NOT EXISTS account(
   accountId INTEGER PRIMARY KEY AUTO_INCREMENT,
   username VARCHAR(50) NOT NULL,
   password CHAR(60) NOT NULL,
   CONSTRAINT usernameUnique UNIQUE (username)
);
CREATE TABLE IF NOT EXISTS thread(
   threadId INTEGER PRIMARY KEY AUTO_INCREMENT,
   threadName VARCHAR(50) NOT NULL,
   threadOfAccount INTEGER NULL,
   FOREIGN KEY (threadOfAccount) REFERENCES account(accountId),
   CONSTRAINT threadNameUnique UNIQUE (threadName)
);
CREATE TABLE IF NOT EXISTS post(
   postId INTEGER PRIMARY KEY AUTO_INCREMENT,
   postTitle VARCHAR(50) NOT NULL,
   postContent TEXT NOT NULL,
   postOnThread INTEGER NOT NULL,
   postOfAccount INTEGER NOT NULL,
   FOREIGN KEY (postOnThread) REFERENCES thread(threadId),
   FOREIGN KEY (postOfAccount) REFERENCES account(accountId),
   CONSTRAINT postTitleUnique UNIQUE (postTitle)
);

我在 sequelize 中定义了相同的表。 暂时添加的协会:

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

当我使用 sequelize 进行包含 LEFT JOIN 的数据库调用时,如下所示:

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

我收到此错误: EagerLoadingError [SequelizeEagerLoadingError]: 线程没有关联到 post!

        post.belongsTo(models.thread, {
            foreignKey: 'postOnThread',
            as: 'thread',
        });
        post.belongsTo(models.account, {
            foreignKey: 'postOfAccount',
            as: 'account',
        });


        thread.belongsTo(models.account, {
            foreignKey: 'threadOfAccount',
            as: 'account',
        });
        thread.hasMany(models.post, {
            foreignKey: 'postOnThread',
            as: 'posts',
        });

测试

        const posts = await db.post.findAll({ include: ['account', 'thread'] });
        console.log(JSON.parse(JSON.stringify(posts)));

        const threads = await db.thread.findAll({ include: ['account', 'posts'] });
        console.log(JSON.parse(JSON.stringify(threads)));

        const accounts = await db.account.findAll({ include: ['account', 'posts'] });
        console.log(JSON.parse(JSON.stringify(accounts)));

我们需要在 associate 定义后调用

db.account.associate(db);
db.post.associate(db);
db.thread.associate(db);