尝试在 Sequelize ORM 中的 queryGenerator.SelectQuery 中添加关联时无法读取未定义的属性“源"

Cannot read property #39;source#39; of undefined when trying to add association in queryGenerator.SelectQuery in Sequelize ORM(尝试在 Sequelize ORM 中的 queryGenerator.SelectQuery 中添加关联时无法读取未定义的属性“源)
本文介绍了尝试在 Sequelize ORM 中的 queryGenerator.SelectQuery 中添加关联时无法读取未定义的属性“源"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 QueryGenerator.selectQuery 生成查询.

I am trying to generate a query using QueryGenerator.selectQuery.

let query = models.sequelize.dialect.QueryGenerator.selectQuery('table', {
    include: [{
        model: models.Users,
        where: {
            deleted: false
        },
        required: true,
        attributes: ['id']
    }],
    where: {
        createdAt: {
            [Op.between]: [o.start, o.end]
        },
        deleted: false
    },
    attributes: [[models.sequelize.fn("COUNT", models.sequelize.col("Table.id")), 'count']]
}, models.Table).slice(0, -1);

这是我遇到的错误.

TypeError:无法读取未定义的属性源"Object.generateJoin(/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1433:30)在 Object.generateInclude(/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1358:24)在 Object.selectQuery(/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1001:34)

TypeError: Cannot read property 'source' of undefined at Object.generateJoin (/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1433:30) at Object.generateInclude (/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1358:24) at Object.selectQuery (/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1001:34)

github 问题跟踪器 https://github.com/sequelize/sequelize/issues/8751

github issue tracker https://github.com/sequelize/sequelize/issues/8751

推荐答案

遇到和你一样的错误,终于解决了问题.

Going through with the same errors as you and finally solve the problem.

因为 QueryGenerator 在内部用于 Sequelize,它没有关于此的特定文档(很遗憾).我们应该解析"在我们将参数传递给 selectQuery 之前使用对象 Model 的选项.

Because QueryGenerator is used internally for Sequelize, it doesn't have specific docs about this (unfortunately). We should "parse" the options using object Model before we passing the parameter into selectQuery.

根据您的查询,我认为您可以这样做.请注意以下代码未经测试并使用 es6 样式

Based on your query, I think you could do this. Please be aware the codes below are untested and use es6 style

// Import sequelize model library
const Model = require("sequelize/lib/model");

// Separate your query options
const queryOptions = {
    include: [{
        model: models.Users,
        where: {
            deleted: false
        },
        required: true,
        attributes: ['id']
    }],
    where: {
        createdAt: {
            [Op.between]: [o.start, o.end]
        },
        deleted: false
    },
    attributes: [[models.sequelize.fn("COUNT", models.sequelize.col("Table.id")), 'count']]
};

// Parse the queryOptions, this operation would serialize the queryOptions
// and this is the important process about building the query
Model._validateIncludedElements.bind(DB.SuitCase)(queryOptions);

// Execute with serialized options object 
let query = models.sequelize.dialect.QueryGenerator.selectQuery('table', queryOptions, models.Table);

希望对您有所帮助.

这篇关于尝试在 Sequelize ORM 中的 queryGenerator.SelectQuery 中添加关联时无法读取未定义的属性“源"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)