Sequelize is owned by ToMany giving TypeError: undefined is not a function
I am using sequelize with expression, I have defined my model as follows
"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;
}
When you try to run the application, you receive the following error message
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 the same as one example given here: https://github.com/sequelize/express-example/blob/master/models/index.js
My product 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've also tried giving ownToMany in app.js, but it doesn't work. All other associations are working correctly. Please, help.
source to share
You have a typo in order.js, you want Item
not Items
. Like this:
Order.belongsToMany(models.Item);
Alternatively, you might get a legacy message asking you to define a parameter through
for this belongsToMany
, like this :
Order.belongsToMany(models.Item, {
through: 'OrderItems'
});
Finally, it belongsToMany
was introduced in the sequel to version 2.1.0, it seems .
source to share