Reputation: 31
I have an association in place for two tables. One is Account, the other, Orders. Orders.belongsTo(Account) and Account.hasMany(Orders). Both tables share a foreign key, 'order_user_id'. I was able to append the foreign key in the class methods like this :
classMethods: {
associate: function(models) {
// associations can be defined here
Orders.belongsTo(models.Accounts, {foreignKey: 'order_user_id'});
}
}
I'm currently passing this query,
models.Orders.findAll({
where: {'orderStatus' : req.query.orderStatus},
limit: req.query.limitTo,
include: [models.Accounts],
order: 'orderDateAdded DESC'
}).then( function (orders, error) {
if(error) {
return res.send(error);
}
console.log(orders);
});
This only returns json containing order details as well as 'order_user_id', but will like to access more attributes of Accounts. How do I achieve this?
Upvotes: 1
Views: 4722
Reputation: 31
I fixed my issue. I was missing required attribute in the include, like this:
include: [ {model: models.Accounts, required: true} ]
That populated Accounts object. :)
Upvotes: 2