编译SELECT查询时检测到未定义的绑定:

时间:2017-06-30 16:49:00

标签: mysql node.js orm bookshelf.js

我是bookshelf.js的新手,我试图从两个表构建一对多的关系,当我想从邮递员那里获取数据时,它会抛出以下错误:

"编译SELECT查询时检测到未定义的绑定:从Q_OBJECT中选择customer_order_line。*,其中customer_order_linecustomer_order_line位于(?)&# 34;

这是customer_order的数据模型:

id_order_line

customer_order_line:

 var customer_order = db.Model.extend({
    tableName: 'customer_order',
    hasTimestamps: true,

    orders : function() {
        return this.hasMany('order_line', 'id_order_line');
    },

    payment : function() {
        return this.belongsTo('payment','id_payment');
    }

})

module.exports = db.model('customer_order', customer_order);

这是我获取数据的函数:

var order_line = db.Model.extend({
    tableName: 'customer_order_line',


    product : function(){
        return this.belongsTo('Product','id_products');
    },

    order : function(){
        return this.belongsTo('customer_order','id_customer_order');
    }
})

module.exports = db.model('order_line', order_line);

mysql表:

 getOrders: function(req, res, next) {
    Customer_orders.forge()
    .fetch({withRelated: ['orders']})
    .then(function (collection){

      res.json({
        error : false,
        data : collection.toJSON()
      });
    })
    .catch(function (err){
      res.status(500)
      .json({
        error : true,
        data: {message: err.message}
      });
    });
  }

1 个答案:

答案 0 :(得分:0)

我修改了 customer_order 模型,如下所示

var customer_order = db.Model.extend({
tableName: 'customer_order',
hasTimestamps: true,

    orders : function() {
       return this.hasMany('order_line', 'id_customer_order');
       //this must contain the foreign key present inside the order_line table and not id_order_line
    },

    payment : function() {
       return this.belongsTo('payment','id_payment');
    }

})
module.exports = db.model('customer_order', customer_order);

如果您的列名不符合 bookshelf.js 命名约定,则必须在指定关系时明确指定主键和外键。

通过Qawelesizwe Mlilo阅读本教程博客nodejs with bookshelfjs mysql