Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The ambiguous error occurs while using the models.sequelize.col () in the include in sequelize(node.js express)

mariadb,

show tables;
Board
Comment

my code,

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));

      });

Why error occurs?

Unhandled rejection SequelizeDatabaseError: ER_NON_UNIQ_ERROR: Column '_no' in field list is ambiguous

models.sequelize.col('_no') -> models.sequelize.col('models.Comment._no') error, too.

models.sequelize.col ( '_ no') in the _no want to use Comment table.

thanks.

like image 889
hyeokluv Avatar asked Oct 22 '25 14:10

hyeokluv


1 Answers

Appearently both Board and Comment have a _no column? In that case you need to specify which one you want to count, fx: models.sequelize.col('board._no')) (make sure the table name matches the pluralization and capitalization of the table in the rest of the query)

like image 171
Jan Aagaard Meier Avatar answered Oct 27 '25 02:10

Jan Aagaard Meier