Sequelize返回" Unknown column"包括表格时

时间:2015-03-25 21:00:10

标签: mysql node.js sequelize.js

这个想法是获得与呼叫服务器上的作业相关联的数字。每个工作都有一堆与之相关的数字。当有人回电时,我需要获得与来电显示相关联的工作。

导致问题的查询:

    // Look for messages to play
    db.job.find( {
        'where': { 'status' : [ 'new', 'inprogress', 'done' ] },
//      'limit' : 1, // Obviously
        'order' : [[ 'jobID' , 'DESC' ]], // Latest
        'include' : [
                     { 
                         'model' : db.number,
                         'where' : { 'number' : this.number }
                     }
                     ]
    } ).complete( ... );

数据库摘录:

var job = sequelize.define( 'job', {
    'id': {
        type: Sequelize.INTEGER( 10 ),
        field: 'job_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'externalID' : {
        type:  Sequelize.STRING( 45 ),
        field: 'external_id',
        allowNull: false,
        unique: true,
        validate: {
            notEmpty: true
        }
    },
    'status' : {
        type:  Sequelize.STRING( 16 ),
        field: 'status',
        allowNull: false,
        defaultValue: 'processing'
    },
    'filename' : {
        type:  Sequelize.STRING( 64 ),
        field: 'filename',
        allowNull: true
    }
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'tableName' : 'jobs'
} );

var number = sequelize.define( 'number', {
    'id': {
        type: Sequelize.BIGINT( 20 ),
        field: 'number_id',
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    'jobID': {
        type: Sequelize.INTEGER( 10 ),
        field: 'job_id',
        allowNull: false
    },
    'externalID' : {
        type:  Sequelize.STRING( 45 ),
        field: 'external_id',
        allowNull: false,
        unique: true,
        validate: {
            notEmpty: true
        }
    },
    'number' : {
        type:  Sequelize.STRING( 16 ),
        field: 'number',
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    'extension' : {
        type:  Sequelize.STRING( 16 ),
        field: 'extension',
        allowNull: true
    },
    'status' : {
        type:  Sequelize.STRING( 16 ),
        field: 'status',
        allowNull: false,
        defaultValue: 'new'
    },
    'lastCall' : {
        type:  Sequelize.DATE,
        field: 'last_call',
        allowNull: true
    },
    'tries' : {
        type:  Sequelize.INTEGER,
        field: 'tries',
        allowNull: false,
        defaultValue: 0
    },
}, {
    'createdAt' : 'created',
    'updatedAt' : 'modified',
    'tableName' : 'numbers'
} );

job.hasMany( number, { 'foreignKey' : 'job_id' } );
number.belongsTo( job, { 'foreignKey' : 'job_id' } );

不知何故,它生成的查询是:

SELECT `job`.*,
       `numbers`.`number_id` AS `numbers.id`,
       `numbers`.`job_id` AS `numbers.jobID`,
       `numbers`.`external_id` AS `numbers.externalID`,
       `numbers`.`number` AS `numbers.number`,
       `numbers`.`extension` AS `numbers.extension`,
       `numbers`.`status` AS `numbers.status`,
       `numbers`.`last_call` AS `numbers.lastCall`,
       `numbers`.`tries` AS `numbers.tries`,
       `numbers`.`created` AS `numbers.created`,
       `numbers`.`modified` AS `numbers.modified`,
       `numbers`.`job_id` AS `numbers.job_id`
FROM
  (SELECT `job`.`job_id` AS `id`,
          `job`.`external_id` AS `externalID`,
          `job`.`status`,
          `job`.`filename`,
          `job`.`created`,
          `job`.`modified`
   FROM `jobs` AS `job`
   WHERE `job`.`status` IN ('new',
                            'inprogress',
                            'done')
     AND
       (SELECT `job_id`
        FROM `numbers` AS `numbers`
        WHERE `job`.`id` = `numbers`.`job_id`
          AND `numbers`.`number` IN ('5556656565') LIMIT 1) IS NOT NULL
   ORDER BY `job`.`jobID` DESC LIMIT 1) AS `job`
INNER JOIN `numbers` AS `numbers` ON `job`.`id` = `numbers`.`job_id`
AND `numbers`.`number` IN ('5556656565')
ORDER BY `job`.`jobID` DESC;

您可以通过表结构看到这个错误:

  

错误:回调:通过号码获取作业时出错。   SequelizeDatabaseError:ER_BAD_FIELD_ERROR:未知列'job.id'   'where clause'

我在哪里错了?

1 个答案:

答案 0 :(得分:1)

实际上似乎有一个关于它的公开问题。

https://github.com/sequelize/sequelize/issues/3403