节点Js(Sequelize)-未处理的拒绝SequelizeDatabaseError:“字段列表”中的未知列“ id”

时间:2019-01-11 11:56:09

标签: mysql node.js sequelize.js findall

我尝试从我的mysql数据库中获取两个列值。

这是我的模特

const Sequelize = require('sequelize');
const db = require('../config/database');

const AuthDetails = db.define('auth_details', {
    client_id : {
        type: Sequelize.STRING
    },
    client_secret : {
        type: Sequelize.STRING
    }
},{
    timestamps : false
});


module.exports = AuthDetails;

而且,这是我的路线

router.post('/login', (req, res, next) => {
// console.log(req.body);

   Users.findOne( { where : { mobile_number: req.body.mobile_number}})
  .then(users => {

  UserAuthDet.findAll({   
    where: {
      client_id: req.body.client_id,
      client_secret: req.body.client_secret
    }
  });
});

从数据库获取client_id和client_secret时出现错误。

我的错误

enter image description here

已更新: Database.js文件

const Sequelize = require('sequelize');

module.exports = new Sequelize('mydbname', 'root', '', {
  host: 'localhost',
  dialect: 'mysql',
  operatorsAliases: false,

  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  },

});

1 个答案:

答案 0 :(得分:2)

尝试将primaryKey: true添加到client_id中的AuthDetails中。

const AuthDetails = db.define('auth_details', {
  client_id : {
    type: Sequelize.STRING,
    primaryKey: true
  },
  client_secret : {
    type: Sequelize.STRING
  }
},{
  timestamps : false
});

我猜Sequelize缺省情况下认为id是主键,除非已指定它,并将id附加到findAll查询中。

ref:https://github.com/sequelize/sequelize/issues/741