在sequelize(node.js express)中的include中使用models.sequelize.col()时出现不明确的错误

hye*_*luv 2 join include mariadb sequelize.js

玛丽亚数据库,

show tables;
Board
Comment
Run Code Online (Sandbox Code Playgroud)

我的代码,

models.Board.findAll({
attributes: [
  '_no', 'title', 'content', 'createdAt'
],
include: [
  {
    model: models.Comment,
    tableAlias: 'Comment',
    attributes: [
      [models.sequelize.fn('count', models.sequelize.col('_no')), 'comment']
    ]
  }
],
group: ['_no', 'title', 'content', 'createdAt'],
order: [
  ['createdAt', 'DESC']
],
    raw: true
   }).then(function(boards)

     {
         res.send(JSON.stringify(boards));

      });
Run Code Online (Sandbox Code Playgroud)

为什么会出现错误?

Unhandled rejection SequelizeDatabaseError: ER_NON_UNIQ_ERROR: Column '_no' in field list is ambiguous
Run Code Online (Sandbox Code Playgroud)

models.sequelize.col('_no') -> models.sequelize.col('models.Comment._no') 也有错误。

models.sequelize.col('_no') 中的_no 想使用Comment 表。

谢谢。

Jan*_*ier 5

显然 Board 和 Comment 都有一_no栏?在这种情况下,您需要指定要计算哪个,fx:(models.sequelize.col('board._no'))确保表名与查询其余部分中表的复数和大写匹配)