问题描述
我正在尝试使用 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 中添加关联时无法读取未定义的属性“源"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!