cphill
cphill

Reputation: 5914

Sequelize Mix-In Errors

I am trying to associate my Images table to my Description table by following the express example of creating two js files for my individual models and then associating the models within an index file. The relationship between the two tables is that Images can only have one Description, but a Description can have multiple Images as shown in my file. Despite following along with the example, I received the following error:

 throw new Error(this.name + '.hasMany called with something that\'s not an
          ^
Error: description.hasMany called with something that's not an instance of Sequelize.Model
    at Mixin.hasMany (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/associations/mixin.js:168:11)
    at Object.<anonymous> (/Users/user/Desktop/Projects/node/assistant/app/models/dbIndex.js:14:13)

Here is my Images model:

module.exports = function(sequelize, DataTypes){

var Images = sequelize.define('images', {
    pattern: DataTypes.STRING,
    color: DataTypes.STRING,
    imageUrl: DataTypes.STRING,
    imageSource: DataTypes.STRING,
    description_id: DataTypes.INTEGER
}, {
    classMethods: {
        associate: function(db) {
            Images.belongsTo(models.description, {foreignKey: 'description_id'});
        }
    }
});
    return Images;
}

Description model:

module.exports = function(sequelize, DataTypes) {

var Description = sequelize.define('description', {
    description_id: {
        type: DataTypes.INTEGER,
        primaryKey: true
    },
    color: DataTypes.STRING,
    body: DataTypes.STRING
});
    return Description;
}

dbIndex model, which joins the two models:

var Sequelize      = require('sequelize');
var sequelize = new Sequelize("db", "admin", "pwd", {
    host: "localhost",
    port: 3306,
    dialect: 'mysql'
});
var db = {};


var Description = sequelize.import(__dirname + "/descriptionModel");

var Images = sequelize.import(__dirname + "/imagesModel");

Description.hasMany('Images');
Images.belongsTo('Description');

module.exports = db;

Upvotes: 2

Views: 1481

Answers (1)

Evan Siroky
Evan Siroky

Reputation: 9408

When you're defining associations for the models with hasMany and belongsTo, you are not following the correct syntax when you send a string instead of a variable that is a sequelize model. That is causing the error you're getting.

I'm assuming you're trying to follow the example here. If you want to import your models one-by-one instead of programmatically searching through a directory, you could modify your index file to this:

var Sequelize      = require('sequelize');
var sequelize = new Sequelize("db", "admin", "pwd", {
    host: "localhost",
    port: 3306,
    dialect: 'mysql'
});

var db = {};
db.Description = sequelize.import(__dirname + "/descriptionModel");
db.Images = sequelize.import(__dirname + "/imagesModel");

db.Images.associate(db);

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

By calling the associate method, you will then call the associate classmethod of the Images model. And you'll want to change the associate classmethod in the Images model as such so you won't get an error:

associate: function(db) {
  Images.belongsTo(db.Description, {foreignKey: 'description_id'});
}

Upvotes: 4

Related Questions