Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ER_BAD_FIELD_ERROR: Unknown column 'id' in 'field list'" sequelize with nodejs

when i try to get data from a certain route in NODEJS thru Sequelize i get the error of unknown column which is neither in model nor route below is my code.

my model

      module.exports = function(sequelize, DataTypes) {
    return sequelize.define('ProspectType', {
      ProspectTypeID: {
        type: DataTypes.INTEGER(11),
        allowNull: false
      },
      ProspectTypeName: {
        type: DataTypes.STRING(50),
        allowNull: true
      }
    }, {
      tableName: 'ProspectType'
    });
  };

my route

        .get('/prospectType', function (req, res) {
        models
            .ProspectType
            .findAll()
            .then(function (data) {
                res
                    .json(data);
            })
            .catch(function (error) {
                res
                    .boom
                    .notAcceptable(error);
            });
    })

even though there is no column 'id' i get this error

SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'id' in 'field 
 list'
like image 859
Venkatesh Avatar asked Oct 15 '25 14:10

Venkatesh


1 Answers

I am assuming that you want ProspectTypeID to be a primary key. However you have not told the sequelize that this is your primary key. Therefore if is looking for a default primary key which is id.

Just declare it as a primary key in your model and you should be good

ProspectTypeID: {
    type: Sequelize.STRING(50),
    allowNull: false,
    primaryKey: true,
  },
like image 62
AbhinavD Avatar answered Oct 17 '25 21:10

AbhinavD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!