3

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
hyeokluv
  • 377
  • 2
  • 8
  • 18

3 Answers3

6

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)

Jan Aagaard Meier
  • 28,078
  • 8
  • 95
  • 66
0

if multiple table is there in query then you need to specify which table createdAt you want to group by because node have all table createdAt(default created)

group: ["your_table_name_here\".\"createdAt"]

if we use multiple model for get record in that case same field name in 2 or more model then you need to specify which model record you want to group by so in that case i write group: ["your_table_name_here"."createdAt"]

  • 1
    Your explanation is really difficult to understand. Please try to clean it up. – General Grievance Aug 01 '22 at 17:23
  • if we use multiple model for get record in that case same field name in 2 or more model then you need to specify which model record you want to group by so in that case i write group: ["your_table_name_here\".\"createdAt"] – Ajay Prajapati Aug 02 '22 at 04:44
0

Try to specify the model where createdAt is located

import { Parking } from "../database/models";

const parking = await Parking.findAndCountAll({
    where: {
      "$Parking.createdAt$": {
         [Op.between]: [new Date(startDate), new Date(endDate)]
      }
    }
})
ncutixavier
  • 455
  • 5
  • 4