mysql - Node Js (Sequelize) - 未处理的拒绝 SequelizeDatabaseError : Unknown column 'id' in 'field list'

标签 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
  },

});

最佳答案

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

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

我猜测 Sequelize 默认将 id 视为主键,除非指定它并将 id 附加到 findAll 查询。

引用:https://github.com/sequelize/sequelize/issues/741

关于mysql - Node Js (Sequelize) - 未处理的拒绝 SequelizeDatabaseError : Unknown column 'id' in 'field list' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54146073/

相关文章:

php - 根据索引回显查询结果

MySQL分区注意事项

node.js - NodeJs : globally installed module not found

javascript - 是否在 NodeJS/Javascript 中实现了任何规则引擎?

mysql - Sequelize - 如何修复 "DataTypes is not defined"?

php - 无法抛出错误消息

mysql - 连接表时如何使用案例条件

python - 安装nodejs时出现"except Error as e SyntaxError"

node.js - 如果sequelize.sync({force :true}) is called之后不存在,则将记录插入表中

javascript - 如何在运行 Node.js 应用程序之前确保 MySQL 数据库存在