Asad
Asad

Reputation: 3160

why model not fetching all attributes in table in sequelize

I have created model for my databse and then run migration it successfully created the table in database after this I create migration to add column to that existing table . When I run model.findall query it only gets the attributes that I created first time e.g here is my model file

'use strict';
 module.exports = {
 up: (queryInterface, Sequelize) => {
 return queryInterface.createTable('ActiveUsers', {
  id: {
    allowNull: false,
    autoIncrement: true,
    primaryKey: true,
    type: Sequelize.INTEGER
  },
  username: {
    type: Sequelize.STRING
  },
  name: {
    type: Sequelize.STRING
  },
  socketId: {
    type: Sequelize.STRING
  },
  createdAt: {
    allowNull: false,
    type: Sequelize.DATE
  },
  updatedAt: {
    allowNull: false,
    type: Sequelize.DATE
  }
   });
 },
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('ActiveUsers');
}
};

here is migration file to add column to this table

'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
queryInterface.addColumn(
  'ActiveUsers',
  'Token',
  {
    type: Sequelize.STRING,
    allowNull: false
  }
   )

  },

  down: (queryInterface, Sequelize) => {

 }
};

here is table pic table it only gets the attributes that are present in model file i.e

username,name,socketId,updatedAt,createdAt 

why it dont get the value of

 token,status

here is my code

 activeusers.findAll({raw:true}).then(Users=>{

      console.log('online users')


    })

Upvotes: 0

Views: 1536

Answers (2)

Guy Segev
Guy Segev

Reputation: 1835

The first file you wrote is not a model file, it is a migration file. If you want to select your new fields you should add them to your model file.

Your model file should look like this:

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('activeUsers', {
        id: {
            type: DataTypes.STRING,
            allowNull: false,
            primaryKey: true,
            unique: true
        },
        username: {
            type: Sequelize.STRING
        },
        name: {            
            type: Sequelize.STRING
        },
        socketId: {
            type: Sequelize.STRING
        },
        token: {
            type: Sequelize.STRING
        },
        status: {
            type: Sequelize.STRING
        },
        createdAt: {
            allowNull: false,
            type: Sequelize.DATE
        },
        updatedAt: {
            allowNull: false,
            type: Sequelize.DATE
        }
    });
};

You can read more in Sequelize docs about how to add models to your project.

Upvotes: 1

Asad
Asad

Reputation: 3160

We have to add column fields to model file manually . then it will fetch that fields

Upvotes: 0

Related Questions