javascript - Cannot read property 'source' of undefined when trying to add association in queryGenerator.SelectQ

I am trying to generate a query using QueryGenerator.selectQuery.let query = models.sequelize.dialect.Q

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);

T‌his is the error I am getting.

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 issue tracker

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);

T‌his is the error I am getting.

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 issue tracker https://github./sequelize/sequelize/issues/8751

Share Improve this question edited Dec 5, 2017 at 11:21 Shareef asked Dec 5, 2017 at 11:07 ShareefShareef 3614 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

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

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.

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);

Hope this may help you.

I would like to add the following to Aditya Kresna Permana's answer;

DB.SuitCase here should be replaced with the model of your top query (Which in the case of the OP would be "models.Table". For people who are familiar with the bind function, this might seem logical (Which is why he might not have added it), but it took me a while to figure out!

Example:

Model._validateIncludedElements.bind(models.Table)(queryOptions);

For future references this worked for me

const Model = require('sequelize/lib/model');

const queryInterface = await db.sequelize.getQueryInterface();
const queryGenerator: any = queryInterface.queryGenerator;

Model._validateIncludedElements.bind(models.TableName)(
  queryObject
);
      
const sqlQuery = queryGenerator.selectQuery(
  'table_name',
   queryObject,
   models.TableName
);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743765012a4503328.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信