【问题标题】:Sequelize included column not recognized in where clauseSequelize 包含的列在 where 子句中不被识别
【发布时间】:2021-08-26 21:55:02
【问题描述】:

我在 Customer 模型中的查询中包含一个名为“FullName”的列,它是 FirstName 和 LastName 的 CONCAT。

const salesEntities = await Sale.findAll({
             subQuery: false,
             include: [{
                 model: Product,
                 where: {
                     RepresentativeId: representativeId
                 },
             }, {
                 model: Warranty,
                 where: warrantyWhereStatements
             },
             {
                 model: Customer,
                 attributes: {
                     include: [[sequelize.fn("CONCAT", sequelize.col("Customer.FirstName",), sequelize.col("Customer.LastName")), "FullName"]],
                 }
             }
             ],
             order: orderOptions,
             where: saleWhereStatements,
             limit: 4,
             offset: page * 4 - 4
         });

当我尝试在 where 子句中使用它时

{ '$Customer.FullName$': search },

它给出了这个错误:'where 子句'中的未知列'Customer.FullName' 有什么建议如何在 where 子句中使用此列?

【问题讨论】:

    标签: javascript node.js sequelize.js


    【解决方案1】:

    您将无法使用 WHERE 子句中的列。

    Sequelize 将根据您使用的参数创建 SQL 查询,并且由于 FullName 列在表中不存在,因此无法在 SQL 查询中使用。

    【讨论】:

    • @StoianDardzhikov 是的,分别搜索名字和姓氏。或者将名字和姓氏组合成一列。
    • 但是在组合列上搜索的唯一方法是使用 sequelize.literal,这会使代码容易受到 SQL 注入的影响。
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2012-06-08
    • 1970-01-01
    • 2013-02-28
    相关资源
    最近更新 更多