Cannot read property #39;source#39; of undefined when trying to add association in queryGenerator.SelectQuery in Sequelize ORM(尝试在 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 中添加关联时无法读取未定义的属性“源"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:尝试在 Sequelize ORM 中的 queryGenerator.SelectQuery 中添加关联时无法读取未定义的属性“源"
基础教程推荐
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01