include in sequelize(node.js express)中使用models.sequelize.col()出现歧义错误
The ambiguous error occurs while using the models.sequelize.col () in the include in sequelize(node.js express)
mariadb,
show tables;
Board
Comment
我的代码,
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));
});
为什么会出错?
Unhandled rejection SequelizeDatabaseError: ER_NON_UNIQ_ERROR: Column '_no' in field list is ambiguous
models.sequelize.col('_no') -> models.sequelize.col('models.Comment._no')
也有错误。
models.sequelize.col('_no')在_no中想使用评论table.
谢谢。
似乎 Board 和 Comment 都有一个 _no
栏?在这种情况下,您需要指定要计数的那个,fx: models.sequelize.col('board._no'))
(确保 table 名称与查询其余部分中 table 的复数形式和大写形式相匹配)
mariadb,
show tables;
Board
Comment
我的代码,
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));
});
为什么会出错?
Unhandled rejection SequelizeDatabaseError: ER_NON_UNIQ_ERROR: Column '_no' in field list is ambiguous
models.sequelize.col('_no') -> models.sequelize.col('models.Comment._no') 也有错误。
models.sequelize.col('_no')在_no中想使用评论table.
谢谢。
似乎 Board 和 Comment 都有一个 _no
栏?在这种情况下,您需要指定要计数的那个,fx: models.sequelize.col('board._no'))
(确保 table 名称与查询其余部分中 table 的复数形式和大写形式相匹配)