小编Jus*_*ber的帖子

查询联结表而不在Sequelize中获取这两个关联

考虑以下模型:

var User = sequelize.define('User', {
  _id:{
    type: Datatypes.INTEGER,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true
  },
  name: Datatypes.STRING,
  email:{
    type: Datatypes.STRING,
    unique: {
      msg: 'Email Taken'
    },
    validate: {
      isEmail: true
    }
  }
});

var Location= sequelize.define('Location', {
  _id:{
    type: Datatypes.INTEGER,
    allowNull: false,
    primaryKey: true,
    autoIncrement: true
  },
  name: Datatypes.STRING,
  address: type: Datatypes.STRING
});

Location.belongsToMany(User, {through: 'UserLocation'});
User.belongsToMany(Location, {through: 'UserLocation'});
Run Code Online (Sandbox Code Playgroud)

有没有办法查询UserLocation表格的具体UserId和获得相应的Locations.就像是:

SELECT * FROM Locations AS l INNER JOIN UserLocation AS ul ON ul.LocationId …

javascript mysql node.js express sequelize.js

9
推荐指数
1
解决办法
772
查看次数

标签 统计

express ×1

javascript ×1

mysql ×1

node.js ×1

sequelize.js ×1