Sequelize JS - 如何使用复合键进行多个连接查询?

时间:2016-11-16 09:12:37

标签: mysql node.js orm associations sequelize.js

我是Sequelize的新手,我一直在为一个简单的查询浪费时间。 sequelize协会让我的头脑旋转@@。

所以我想运行这个查询

'use strict';
module.exports = function(sequelize, DataTypes) {
  var states = sequelize.define('states', {
    botName: DataTypes.STRING,
    chatId: DataTypes.INTEGER,
    state: DataTypes.STRING,
    turnId: DataTypes.INTEGER,
    turnName: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        states.hasOne(models.rooms, {
          as: 'rooms',
          foreignKey: {
            name: 'stateId'
          },
          foreignKeyConstraint: true
        });
        // associations can be defined here
      }
    }
  });
  return states;
};

我不知道如何以续集格式实现这种方式。 这是我的模特:

states.js

'use strict';
module.exports = function(sequelize, DataTypes) {
  var rooms = sequelize.define('rooms', {
    stateId: DataTypes.INTEGER,
    hostId: DataTypes.INTEGER,
    maxLetter: DataTypes.INTEGER
  }, {
    classMethods: {
      associate: function(models) {
        rooms.belongsTo(models.states, {
          as: 'states',
          foreignKey: {
            name: 'stateId'
          },
          foreignKeyConstraint: true
        });
        rooms.hasMany(models.roomdetails, {
          as: 'roomdetails',
          foreignKey: {
            name: 'roomId'
          },
          foreignKeyConstraint: true
        });
        rooms.belongsToMany(models.roomdetails, {
          as: 'roomdetailschatid',
          foreignKey: {
            name: 'hostId'
          },
          foreignKeyConstraint: true
        });
        // associations can be defined here
      }
    }
  });
  return rooms;
};

rooms.js

'use strict';
module.exports = function(sequelize, DataTypes) {
  var roomdetails = sequelize.define('roomdetails', {
    roomId: DataTypes.INTEGER,
    chatId: DataTypes.INTEGER,
    firstName: DataTypes.STRING,
    lastName: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        roomdetails.belongsTo(models.rooms, {
          as: 'rooms',
          foreignKey: {
            name: 'roomId'
          },
          foreignKeyConstraint: true
        });
        roomdetails.belongsTo(models.rooms, {
          as: 'roomshostid',
          foreignKey: {
            name: 'chatId'
          },
          foreignKeyConstraint: true
        });
        // associations can be defined here
      }
    }
  });
  return roomdetails;
};

roomdetails.js

module.exports.getHostInfo = function(res) {
    return models.roomdetails.findOne({
        include: {
            required: true,
            model: models.rooms,
            as: 'rooms',
            include: {
                required: true,
                model: models.states,
                as: 'states',
                where: {
                    botName: { $eq: general.botName },
                    chatId: { $eq: res.chat.id },
                    state: { $ne: general.FINISHED}
                }
            }
        }
    })
    .then(function (response) {
        console.log(response);
        return response;
    })
    .catch(function (error) {
        console.log('error getTotalPlayer', error);
    });
}

这是我到目前为止所尝试的

{{1}}

并且它总是会返回错误,例如“字段列表”中的未知列'rooms.roomdetailId'

任何人都可以帮助我吗? 任何帮助将不胜感激,谢谢

2 个答案:

答案 0 :(得分:0)

正如错误所说,Sequelize在使用snake_case保存的数据库列中找不到camelCase的列。尝试使用snake_case定义foreignKey:

    rooms.hasMany(models.roomdetails, {
      as: 'roomdetails',
      foreignKey: 'room_id',
      foreignKeyConstraint: true
    });

答案 1 :(得分:0)

您是否在models / index.js中定义了关联?如果没有定义如下

sequelize.roomDetails.belongsTo(sequelize.roome);
sequelize.rooms.hasMany(sequelize.roomDetails, {as: 'roomDetails', foreignKey: 'room_id'});

然后你可以使用

rooms.hasMany(models.roomdetails, {
  as: 'roomdetails',
  foreignKey: 'room_id',
  foreignKeyConstraint: true
});

希望这会对你有帮助!