javascript - 关联查询中如何指定外键

标签 javascript mysql node.js foreign-keys sequelize.js

当我进行包含另一个模型的查询时,如何指定外键,因为我有该模型的许多外键。

DBTweet.findAll({ where: { userID: followingUserID }, include: [
            { model: DBUser, 
        include: [
                { model: DBFollower // Here i want to specify the foreign key },
        ] },
]})

更新:

当我与 as 有两个关联时

users is associated to followers multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include

DBFollower.findAll({ where: { followerUserID: req.params.userID }, include: [ 
    { model: DBUser, attributes: { exclude: ['email', 'password', 'loginType', 'telephoneNumber'] }}
]})

这些是我的联想:

DBUser.hasMany(DBTweet, { foreignKey: 'userID' }, { onDelete: 'cascade' })
DBTweet.belongsTo(DBUser, {foreignKey: 'userID'}, { onDelete: 'cascade' })

DBUser.hasMany(DBFollower, { as: 'followingUserIDAlias', foreignKey: 'followingUserID' }, { onDelete: 'cascade' })
DBFollower.belongsTo(DBUser, { as: 'followingUserIDAlias', foreignKey: 'followingUserID' }, { onDelete: 'cascade' })

DBUser.hasMany(DBFollower, { as: 'followerUserIDAlias', foreignKey: 'followerUserID' }, { onDelete: 'cascade' })
DBFollower.belongsTo(DBUser, { as: 'followerUserIDAlias', foreignKey: 'followerUserID' }, { onDelete: 'cascade' })

最佳答案

DBTweet.findAll({ 
  where: { userID: followingUserID }, 
  include: [{ 
    model: DBUser,
    as: 'Users', //here goes the alias as well
    include: [{ 
      model: DBFollower, 
      as: 'Followers' //here is goes the alias of the association
    }],
  }]
});


module.exports = (sequelize, DataTypes) => {
  const DBUser = sequelize.define('DBUser', {
    // your attributes
  });

  DBUser.associate = (models) => {
    DBUser.hasMany(models.DBFollower,  {  as: 'Followers',  foreignKey: 'your_key' });
    // DBUser.belongsTo(models.DBFollower,  {  as: 'Followers',  foreignKey: 'branch_id' });
  };

  return DBUser;
};

更新:

现在与您的协会:

DBUser.hasMany(DBTweet, { as: 'Users', foreignKey: 'userID', onDelete: 'cascade' })
DBTweet.belongsTo(DBUser, { as: 'Users', foreignKey: 'userID', onDelete: 'cascade' })

DBUser.hasMany(DBFollower, { as: 'followingUserIDAlias', foreignKey: 'followingUserID', onDelete: 'cascade' })
DBFollower.belongsTo(DBUser, { as: 'followingUserIDAlias', foreignKey: 'followingUserID', onDelete: 'cascade' })

DBUser.hasMany(DBFollower, { as: 'followerUserIDAlias', foreignKey: 'followerUserID', onDelete: 'cascade' })
DBFollower.belongsTo(DBUser, { as: 'followerUserIDAlias', foreignKey: 'followerUserID', onDelete: 'cascade' })

关于javascript - 关联查询中如何指定外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56174796/

相关文章:

javascript - 将类应用于前 5 行和后 5 行

mySQL查询,合并两行并添加另一列作为输出结果

c# - 如何使用 C# 在 app.config 中为 mysql 数据库提供默认连接字符串

node.js - Node net.createConnection ECONNREFUSED

javascript - For 循环没有按预期工作,我做错了什么?

javascript - 用户单击关闭按钮后取消 HTA 关闭

javascript - 与多个状态变量 react 的通用函数

php - 使用 PHP :date() or MySQL:FROM_UNIXTIME()? 存储时间戳

javascript - 为什么 JavaScript 的逻辑 AND (&&) 与 if 条件不一样?

angularjs - 如何使用mongoose原生的 promise (mpromise)删除一个文档然后更新另一个文档