2
votes

I am using my existing database which has no foreign keys created but I am able to join two tables using sql query but I am not able to join them in sequelize.

There are two models: - User:

module.exports = (sequelize, DataTypes) => {
  var User = sequelize.define('User', {
    steamid: DataTypes.STRING,
    name: DataTypes.STRING,
    img: DataTypes.STRING,
    tradelink: DataTypes.STRING,
    ban_chat: DataTypes.INTEGER,
    block_sms: DataTypes.INTEGER,
    balance: DataTypes.INTEGER,
    ref: DataTypes.STRING,
    refcode: DataTypes.STRING,
    ip_address: DataTypes.STRING
  }, {
    timestamps: false
  });
  User.associate = function(models) {
    // associations can be defined here
    User.hasMany(models.Order,{
      foreignKey: 'steamid',
      as: 'orders'
    });

  };
  return User;
};
  • Order:
const customDataTypes = require('../../core').SequelizeTimestamp;

module.exports = (sequelize, DataTypes) => {
  var Order = sequelize.define('Order', {
    steamid: DataTypes.STRING,
    item_name: DataTypes.STRING,
    price: DataTypes.FLOAT,
    type: DataTypes.STRING,
    website: DataTypes.STRING,
    amount: DataTypes.INTEGER,
    status: DataTypes.INTEGER,
    img: DataTypes.STRING,
    send_attempts: DataTypes.INTEGER,
    message: DataTypes.STRING,
    date: customDataTypes.TIMESTAMP,
  }, {
    timestamps: false
  });
  Order.associate = function(models) {
    // associations can be defined here
    Order.belongsTo(models.User, {
      foreignKey: 'steamid',
      as: 'user'
    })
  };
  return Order;
};

In my API i want to get user and his orders. So in the controller i am doing:

user.findOne({
      where: { steamid: req.params.steamId },
      include: [{
        model: order,
        as: 'orders',
        limit: 50
      }],
    })

So I expect to have user order array in the response but for some reason I get empty orders array.

Sequelize is doing these two queries:

SELECT `User`.`id`, `User`.`steamid`, `User`.`name`, `User`.`img`, `User`.`tradelink`, `User`.`ban_chat`, `User`.`block_sms`, `User`.`balance`, `User`.`ref`, `User`.`refcode`, `User`.`ip_address` FROM `Users` AS `User` WHERE `User`.`steamid` = '1234' LIMIT 1;

This is successfully finding the user but the second query is incorrect:

SELECT `id`, `steamid`, `item_name`, `price`, `type`, `website`, `amount`, `status`, `img`, `send_attempts`, `message`, `date` FROM `Orders` AS `Order` WHERE `Order`.`steamid` IN (1) LIMIT 50;

This part is incorrect "WHERE Order.steamid IN (1)" It is looking for orders which steamid is = 1 which is user id (primary key) but it should be user steamid which is "1234"

What is wrong with my associations?

1

1 Answers

1
votes

If steamid is not a primary you should`t be using "targetKey" and "sourceKey"?

https://sequelize.org/master/manual/associations.html

   Order.belongsTo(models.User, {
      foreignKey: 'steamid',
      targetKey: 'steamid',
      as: 'user'
    })
    Order.belongsTo(models.User, {
      foreignKey: 'steamid',
      sourceKey: 'steamid',
      as: 'user'
    })