Reputation: 606
I am using sequelize with express, I have defined my model like this
"use strict";
var Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
var Order = sequelize.define("Order",
{
status: {
type: Sequelize.INTEGER
},
notes: {
type: Sequelize.STRING
}
},
{
classMethods: {
associate: function(models) {
Order.belongsTo(models.User);
Order.belongsTo(models.Address);
Order.belongsToMany(models.Items);
}
}
}
);
return Order;
}
I am getting following error message when trying to run my app
Order.belongsToMany(models.Items );
^
TypeError: undefined is not a function
at sequelize.define.classMethods.associate (models/order.js:18:7)
at models/index.js:24:19
at Array.forEach (native)
at Object. (models/index.js:22:17)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
My index.js file is same as one given is express example here: https://github.com/sequelize/express-example/blob/master/models/index.js
My item model looks like this:
"use strict";
var Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
var Item = sequelize.define("Item", {
title: {
type: Sequelize.STRING
},
mrp: {
type: Sequelize.DECIMAL,
allowNull: false
},
sp: {
type: Sequelize.DECIMAL,
allowNull: false
},
desc: {
type: Sequelize.STRING
},
skuId: {
type: Sequelize.STRING,
allowNull: false
},
type: {
type: Sequelize.STRING
},
merchantId: {
type: Sequelize.STRING,
allowNull: false
},
categoryId: {
type: Sequelize.STRING,
allowNull: false
}
}
);
return Item;
}
I have also tried giving belongsToMany in app.js but it does not work. All other associations are working correctly. Please help.
Upvotes: 4
Views: 5370
Reputation: 8141
You've got a typo in order.js, you want Item
not Items
. Like this:
Order.belongsToMany(models.Item);
Also, you might get a deprecated message asking you to define the through
param for this belongsToMany
, eg like this:
Order.belongsToMany(models.Item, {
through: 'OrderItems'
});
Lastly, belongsToMany
was introduced in sequelize version 2.1.0, it seems.
Upvotes: 3