在Node Sequelize- Unknown column' resortReview.rating'中获取以下错误在'字段列表'

时间:2018-03-08 06:58:37

标签: mysql node.js sequelize.js

尝试执行以下查询

> Resort.findAndCountAll(
>         {
>             where: { status: "active", name: { [Sequelize.Op.like]: '%' + _search_like + '%' } },
>             attributes: [
>                 "id",
>                 "name",
>                 "email",
>                 "mobileNumber",
>                 "averagePrice",
>                 "description",
>                 "rating",
>                 "location",
>                 "city",
>                 "country",
>                 [Sequelize.fn('AVG', Sequelize.col('resortReview.rating')), 'total'],
>                 [models.sequelize.literal(`Concat('` + imageHelper.SERVER_IMAGE_ROOT_URL + `',image)`), 'image']],
>             include: [{
>                 model: models.resortReview,
>                 required : false,
>                 attributes: []
>             }],
>             limit: limit,
>             offset: offset,
>             group: ["resort.id"],
>             order: [
>                 [sort, order],
>             ]
>         }
>     )

请找到该协会,

db.resortReview.belongsTo(db.resort, {onDelete : 'CASCADE'});
db.resort.hasMany(db.resortReview, {foreignKey : 'resortId'});

但是我遇到了以下错误

  

未处理拒绝SequelizeDatabaseError:未知列   ' resortReview.rating'在'字段列表'

生成查询: -

SELECT `resort`.* 
FROM   (SELECT `resort`.`id`, 
               `resort`.`name`, 
               `resort`.`email`, 
               `resort`.`mobilenumber`, 
               `resort`.`averageprice`, 
               `resort`.`description`, 
               `resort`.`rating`, 
               `resort`.`location`, 
               `resort`.`city`, 
               `resort`.`country`, 
               Concat('http://hamsontech.com:9093/images/', image) AS `image`, 
               Avg(`resortreviews`.`rating`)                       AS `total` 
        FROM   `resorts` AS `resort` 
        WHERE  `resort`.`status` = 'active' 
               AND `resort`.`name` LIKE '%%' 
        GROUP  BY `resort`.`id` 
        ORDER  BY `resort`.`name` ASC 
        LIMIT  0, 10) AS `resort` 
       LEFT OUTER JOIN `resortreviews` AS `resortReviews` 
                    ON `resort`.`id` = `resortreviews`.`resortid` 
ORDER  BY `resort`.`name` ASC; 

不知道我错过了什么?

2 个答案:

答案 0 :(得分:0)

我认为您的问题是您未在联接表的属性列表中包含rating。试一试:

Resort.findAndCountAll({
    where: { status: "active", name: { [Sequelize.Op.like]: '%' + _search_like + '%' } },
    attributes: [
        "id",
        "name",
        "email",
        "mobileNumber",
        "averagePrice",
        "description",
        "location",
        "city",
        "country",
        [Sequelize.fn('AVG', Sequelize.col('resortReview.rating')), 'total'],
        [models.sequelize.literal(`Concat('` + imageHelper.SERVER_IMAGE_ROOT_URL + `',image)`), 'image']
    ],
    include: [{
        model: models.resortReview,
        required : false,
        attributes: ["rating"]
    }],
    limit: limit,
    offset: offset,
    group: ["resort.id"],
    order: [
        [sort, order],
    ]
})

如果启用Sequelize logging选项并查看它生成的原始查询,也会有所帮助。这应该可以帮助您跟踪问题。

答案 1 :(得分:0)

这里唯一产生问题的是:

limit: limit,
offset: offset,
group: ["resort.id"],
order: [
    [sort, order],
]

尝试删除limit: limit,offset: offset或也可以order,然后尝试。

有关详情: READ