Filipe Ferminiano
Filipe Ferminiano

Reputation: 8799

called with something that's not an instance of Sequelize.Model at Model.belongsTo

I'm trying to refer a foreign key between 2 models.

but I'm getting this error:

throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not an instance of Sequelize.Model');
called with something that's not an instance of Sequelize.Model
    at Model.belongsTo

How can I fix this?

This is my code so far.

This is my models/mercadolibre.js

"use strict";
var User  = require('../models/index').User;

module.exports = function(sequelize, DataTypes) {
  var MercadoLibre = sequelize.define("MercadoLibre", {
    id:  { 
          type: DataTypes.INTEGER, 
          autoIncrement: true, 
          primaryKey: true
        },
    access_token: DataTypes.STRING,
    refresh_token: DataTypes.STRING,
    environment_hash: DataTypes.STRING 
  }, {
    tableName: 'mercadolibres',
    underscored: true,
    timestamps: true
  }

  );

  MercadoLibre.belongsTo(User);

  return MercadoLibre;
};

This is my models/user.js

"use strict";

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    id:  { 
          type: DataTypes.INTEGER, 
          autoIncrement: true, 
          primaryKey: true
        },
    name: DataTypes.STRING,
    slack_id: DataTypes.STRING,
    environment_hash: {
          type: DataTypes.STRING,
          defaultValue: DataTypes.UUIDV4
        }
  }, {
    tableName: 'users',
    underscored: false,
    timestamps: false
  }

  );

  return User;
};

This is my models/index.js

"use strict";

var fs        = require("fs");
var path      = require("path");
var Sequelize = require('sequelize')
  , sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
      dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
      port:    3306, // or 5432 (for postgres)
});


var db = {};
fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf(".") !== 0) && (file !== "index.js");
  })
  .forEach(function(file) {
    var model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if ("associate" in db[modelName]) {
    db[modelName].associate(db);
  }
});

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

module.exports = db;

Upvotes: 3

Views: 7168

Answers (1)

farhadamjady
farhadamjady

Reputation: 982

use like this:

module.exports = function(sequelize, DataTypes) {
var MercadoLibre = sequelize.define("MercadoLibre", {
   id:  { 
      type: DataTypes.INTEGER, 
      autoIncrement: true, 
      primaryKey: true
    },
    access_token: DataTypes.STRING,
    refresh_token: DataTypes.STRING,
    environment_hash: DataTypes.STRING 
}, {
    tableName: 'mercadolibres',
    underscored: true,
    timestamps: true,
    classMethods: {
        associate : function(models) {
            MercadoLibre.belongsTo(models.User)
        },
      },
  });



return MercadoLibre;

};

Upvotes: 9

Related Questions