title: How to use sequelize to fetch object data?
body: <p>I am trying to implement <a href="http://docs.sequelizejs.com/manual/associations.html" rel="nofollow noreferrer">Sequelize</a> as an ORM in NodeJs and I am using it for Mysql,<br>
  <strong>I have 3 tables in the sample -</strong><br><br>
  1. Role<br>
  2. User (Has a role)<br>
  3. Code (IsCreated by a user)<br></p>
  
  <p>I'm unable to query the tables/models properly,<br>
  As I should be receiving an model representation of a table, which is referred as a foreign key.</p>
  
  <p><strong>Following is my DB structure -</strong><br>
  <strong>1. Role table -</strong><br>
  <a href="https://i.stack.imgur.com/s1Bqu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s1Bqu.png" alt="enter image description here"></a></p>
  
  <p><strong>2. User table -</strong><br>
  <a href="https://i.stack.imgur.com/aTbhr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aTbhr.png" alt="enter image description here"></a></p>
  
  <p><strong>3. Code table -</strong><br>
  <a href="https://i.stack.imgur.com/Kj8lg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Kj8lg.png" alt="enter image description here"></a></p>
  
  <p><strong>Following are the table creation queries -</strong><br>
  <strong>1. Role -</strong>
      CREATE TABLE <code>role</code> (
        <code>id</code> int(11) NOT NULL AUTO_INCREMENT,
        <code>name</code> varchar(100) NOT NULL,
        PRIMARY KEY (<code>id</code>)
      );</p>
  
  <p><strong>2. User -</strong></p>
  
  <pre><code>CREATE TABLE `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `email` varchar(100) NOT NULL,
    `role` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `role` (`role`),
    CONSTRAINT `user_ibfk_1` FOREIGN KEY (`role`) REFERENCES `role` (`id`)
  );
  </code></pre>
  
  <p><strong>3. Code -</strong></p>
  
  <pre><code>CREATE TABLE `code` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(100) NOT NULL,
    `createdBy` int(11) DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `createdBy` (`createdBy`),
    CONSTRAINT `code_ibfk_1` FOREIGN KEY (`createdBy`) REFERENCES `user` (`id`)
  );
  </code></pre>
  
  <p><strong>Following is my app.js file -</strong><br></p>
  
  <pre><code> const db = require('./db');
  const Code = require('./code');
  const User = require('./user');
  const Role = require('./role');
  const async = require('async');
  
  
  async.waterfall([
      function(callback) {
          db
          .authenticate()
          .then(() =&gt; {
              console.log('Connection has been established successfully.');
              callback(null,"ok");
          })
          .catch(err =&gt; {
              return callback(err,null);
          });        
      },
      function(resp,callback) {
          Code.findAll({include: [{ model: User}]})
          .then(code =&gt; {
              console.log("All users:", JSON.stringify(code, null, 4));
              callback(null,"ok");
          })
          .catch(err =&gt; {
              callback(err,null);
          });
  
          // Code.findOne({
          //     where: {
          //         id: 1
          //     }
          // })
          // .then(code =&gt; {
          //     console.log("All users:", JSON.stringify(code, null, 4));
          // })
          // .catch(err =&gt; console.log("Error =&gt; \n",err));
  
      },
  ],
  function(err, resp) {
      if (err) {
          console.log(err);
      } else {
          console.log(resp);
      }
  });
  </code></pre>
  
  <p><strong>Following is my db.js file -</strong><br></p>
  
  <pre><code>const Sequelize = require('sequelize');
  
  module.exports =  new Sequelize('junit', 'root', 'root', {
      host: 'localhost',
      /* one of 'mysql' | 'mariadb' | 'postgres' | 'mssql' */
      dialect: 'mysql',
      //operatorsAliases: false,
      pool: {
        max: 5,
        min: 0,
        acquire: 30000,
        idle: 10000
      }
    });
  </code></pre>
  
  <p><strong>Following is my role.js file -</strong><br></p>
  
  <pre><code>const Sequelize = require('sequelize');
  const db = require('./db');
  const User = require('./user');
  
  const Role = db.define('role', {
      id: {
          type: Sequelize.INTEGER,
          primaryKey: true,
          allowNull:false,
          autoIncrement: true
      },
      name: {
        type: Sequelize.STRING
      }
    }, {
      tableName: 'role',
      freezeTableName: true,
      timestamps: false
  });
  
  associate : (models) =&gt; {
      Role.hasMany(models.User,{
          foreignKey: 'role'
      });
  };
  
  module.exports = Role;
  </code></pre>
  
  <p><strong>Following is my user.js file -</strong><br></p>
  
  <pre><code>const Sequelize = require('sequelize');
  const db = require('./db');
  const Code = require('./code');
  const Role = require('./role');
  
  const User = db.define('user', {
      id: {
          type: Sequelize.INTEGER,
          primaryKey: true,
          allowNull:false,
          autoIncrement: true
      },
      name: {
        type: Sequelize.STRING
      },
      email: {
          type: Sequelize.STRING
      },
      role: {
          type: Sequelize.INTEGER,
          references: {
            // This is a reference to another model
            model: Role,
  
            // This is the column name of the referenced model
            key: 'id'
          }
        }
    }, {
      tableName: 'user',
      freezeTableName: true,
      timestamps: false
  });
  
  associate : (models) =&gt; {
      User.hasMany(models.Code,{
          foreignKey: 'createdBy'
      });
  
      User.belongsTo(models.Role,{
          foreignKey: 'role'
      });
  };
  
  module.exports = User;
  </code></pre>
  
  <p><strong>Following is my code.js file -</strong><br></p>
  
  <pre><code>const Sequelize = require('sequelize');
  const db = require('./db');
  const User = require('./user');
  //one-to-many
  const Code = db.define('code', {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      allowNull:false,
      autoIncrement: true
    },
    name: {
      type: Sequelize.STRING
    },
    // createdBy: {
    //   type: Sequelize.INTEGER
    // },
    createdBy: {
      type: Sequelize.INTEGER,
      references: {
        // This is a reference to another model
        model: User,
  
        // This is the column name of the referenced model
        key: 'id'
      }
    }
  }, {
    tableName: 'code',
    freezeTableName: true,
    timestamps: false
  });
  
  associate : (models) =&gt; {
    Code.belongsTo(models.User,{
        foreignKey: 'createdBy'
    });
  };
  
  module.exports = Code;
  </code></pre>
  
  <p><strong>When I run the app.js file I can't see the model reference of User,<br>
  But I get the usual Integer value, Can someone please help on how to properly use the Model here?</strong><br></p>
  
  <p><strong>Error trace -</strong>
  <a href="https://i.stack.imgur.com/VOeEz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VOeEz.png" alt="enter image description here"></a></p>