Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在关联查询中指定外键_Javascript_Mysql_Node.js_Foreign Keys_Sequelize.js - Fatal编程技术网

Javascript 如何在关联查询中指定外键

Javascript 如何在关联查询中指定外键,javascript,mysql,node.js,foreign-keys,sequelize.js,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 }, ] }, ]})

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

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

当我与as有两种关联时

用户多次与关注者关联。要标识正确的关联,必须使用“as”关键字指定要包含的关联的别名

这些是我的联想:

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' })
更新:

现在请大家联系:

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' })

从DBUser到DBFollower,我有两个hasMany与as的关联:我得到一个错误:用户多次与followers关联。若要标识正确的关联,必须使用“as”关键字指定要包含的关联的别名。如错误所示,您需要在每个关联上放置别名。为了避免这种情况,请始终使用别名。如果我按如下操作:在DBFollower findAll查询中,我会得到相同的错误。再次参见问题A请使用
DBTweet
DBUser
DBFollower
的关联进行更新,再次参见问题
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' })