Reputation: 1636
In a shopping cart management system, User has manyOrders and an order belongsTo a user, in this case userID(PK of 'User' Table) is FK in 'Order' table. I tried this but this gives association error that order is not associated to user.
db.user.findAll({
include: [
{
model: db.order
}
]
})
.then(users => {
res.json(users)
});
Here are the models :-
Order :-
'use strict';
var db = require('../models/index');
module.exports = function (sequelize, DataTypes) {
var order = sequelize.define('order', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
userId: {
type: DataTypes.INTEGER,
allowNull: false
},
totalAmount: {
type: DataTypes.INTEGER,
allowNull: false
}
,
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
}
}, {
classMethods: {
associate: function (models) {
order.belongsTo(models.user);
order.belongsToMany(models.products, { through: { model: models.orderProduct } });
// order.hasMany(models.orderProduct);
}
}
});
return order;
};
User :-
'use strict';
var db = require('../models/index');
module.exports = function (sequelize, DataTypes) {
var user = sequelize.define('user', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(255),
allowNull: false
},
email: {
type: DataTypes.STRING(255),
allowNull: false
},
password: {
type: DataTypes.STRING(255),
allowNull: false
},
address: {
type: DataTypes.STRING(255),
allowNull: true
},
contact: {
type: DataTypes.STRING(255),
allowNull: true
},
type: {
type: DataTypes.STRING(255),
allowNull: true
},
createdAt: {
type: DataTypes.DATE,
allowNull: true
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true
},
}, {
activated: {
type: DataTypes.BOOLEAN,
allowNull: true
}
}, {
classMethods: {
associate: function (models) {
user.hasMany(models.order);
}
}
});
return user;
};
Here is the error :-
Unhandled rejection Error: order is not associated to user!
at Model.validateIncludedElement (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:558:11)
at F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:440:29
at Array.map (native)
at Model.validateIncludedElements (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:436:37)
at Model.<anonymous> (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\sequelize\lib\model.js:1372:32)
at Model.tryCatcher (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:569:18)
at Promise._settlePromise0 (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:614:10)
at Promise._settlePromises (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\promise.js:693:18)
at Async._drainQueue (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\async.js:133:16)
at Async._drainQueues (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\async.js:143:10)
at Immediate.Async.drainQueues (F:\UNI\BSCS -VII\IAD\WebProjects\Project\SERVER\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:666:20)
at tryOnImmediate (timers.js:639:5)
at processImmediate [as _immediateCallback] (timers.js:611:5)
Upvotes: 1
Views: 1601
Reputation: 1090
Try this. Modify your associations like this:
order.belongsTo(models.user, {as : 'User'});
user.hasMany(models.order, {as : 'Orders'});
And while querying:
db.user.findAll({
include: [{model: db.order, as: 'Orders'}]
})
.then(users => {
res.json(users)
});
Upvotes: 0
Reputation: 5041
Specify your foreign keys. Those keys you are using won't be automagically deduced.
order.belongsTo(models.user, {foreignKey: 'userID'});
..
user.hasMany(models.order, {foreignKey: 'userID'});
EDIT: I rechecked your post, your user is defined in the wrong way. Your last field "activated" is outside, closing the previous object. That's why it's not reading your association.
updatedAt: {
type: DataTypes.DATE,
allowNull: true
},
}, {
activated: {
type: DataTypes.BOOLEAN,
allowNull: true
}
}, {
classMethods: {
associate: function (models) {
user.hasMany(models.order);
}
}
});
Should be:
updatedAt: {
type: DataTypes.DATE,
allowNull: true
},
activated: {
type: DataTypes.BOOLEAN,
allowNull: true
}
}, {
classMethods: {
associate: function (models) {
user.hasMany(models.order);
}
}
});
Upvotes: 1