Javascript 使用BookshelfJS/KnexJS的NodeJS脚本中的基本表关联

Javascript 使用BookshelfJS/KnexJS的NodeJS脚本中的基本表关联,javascript,mysql,node.js,Javascript,Mysql,Node.js,我的第一个BookshelfJS/KnexJS脚本有一些问题,它有一个表关联。我非常仔细地复制了一对多的例子,只是将它从书本和页面切换到了干燥器和汽车。但是,每当我执行查询并包含withRelated项时,它都不会返回关联的数据(在本例中是汽车),它只返回驱动程序 该协会是许多“汽车”的“司机”。我甚至在演示中使用了KnexJS脚本来创建表,因此它们与示例表几乎相同。下面是JS脚本: “严格使用”; const Config=require('./Config'); const Knex=req

我的第一个BookshelfJS/KnexJS脚本有一些问题,它有一个表关联。我非常仔细地复制了一对多的例子,只是将它从书本和页面切换到了干燥器和汽车。但是,每当我执行查询并包含withRelated项时,它都不会返回关联的数据(在本例中是汽车),它只返回驱动程序

该协会是许多“汽车”的“司机”。我甚至在演示中使用了KnexJS脚本来创建表,因此它们与示例表几乎相同。下面是JS脚本:

“严格使用”;
const Config=require('./Config');
const Knex=require('Knex')(require('./config').database);
Knex.schema
.createTable('驱动程序',函数(表){
表.增量('driver_id').primary();
table.string('name');
表1.timestamps();
})
.createTable('cars',函数(表){
//使用Myisam,因为Innodb抛出了一个错误
表1.发动机(“myisam”);
表.增量('car_id').primary();
表.integer('driver_id')。引用('drivers.driver_id');
table.string('make');
table.string('model');
表.整数('年');
表1.timestamps();
})
.then(功能(数据){
console.log('DONE',data);
})
.catch(函数(err){
console.log('ERROR',err);
});
更多信息,请参阅以下表格结构和表格内容:

mysql> explain drivers;
 +------------+------------------+------+-----+---------+----------------+
 | Field      | Type             | Null | Key | Default | Extra          |
 +------------+------------------+------+-----+---------+----------------+
 | driver_id  | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
 | name       | varchar(255)     | YES  |     | NULL    |                |
 | created_at | datetime         | YES  |     | NULL    |                |
 | updated_at | datetime         | YES  |     | NULL    |                |
 +------------+------------------+------+-----+---------+----------------+
 4 rows in set (0.00 sec)

 mysql> explain cars;
 +------------+------------------+------+-----+---------+----------------+
 | Field      | Type             | Null | Key | Default | Extra          |
 +------------+------------------+------+-----+---------+----------------+
 | car_id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
 | driver_id  | int(11)          | YES  | MUL | NULL    |                |
 | make       | varchar(255)     | YES  |     | NULL    |                |
 | model      | varchar(255)     | YES  |     | NULL    |                |
 | year       | int(11)          | YES  |     | NULL    |                |
 | created_at | datetime         | YES  |     | NULL    |                |
 | updated_at | datetime         | YES  |     | NULL    |                |
 +------------+------------------+------+-----+---------+----------------+
 7 rows in set (0.00 sec)

 mysql> select * from drivers;
 +-----------+----------+---------------------+------------+
 | driver_id | name     | created_at          | updated_at |
 +-----------+----------+---------------------+------------+
 |         1 | John Doe | 2015-12-17 00:00:00 | NULL       |
 |         2 | The Stig | 2015-12-08 00:00:00 | NULL       |
 +-----------+----------+---------------------+------------+
 2 rows in set (0.00 sec)

 mysql> select * from cars;
 +--------+-----------+-----------+--------+------+---------------------+------------+
 | car_id | driver_id | make      | model  | year | created_at          | updated_at |
 +--------+-----------+-----------+--------+------+---------------------+------------+
 |      1 |         1 | Chevrolet | Camaro | 2014 | 2015-12-18 00:00:00 | NULL       |
 |      2 |         1 | Acura     | RSX-S  | 2004 | 2015-12-11 00:00:00 | NULL       |
 |      3 |         2 | Ford      | Focus  | 2004 | 2015-12-18 00:00:00 | NULL       |
 |      4 |         2 | Nissan    | Maxima | 2001 | 2015-12-17 00:00:00 | NULL       |
 |      5 |         2 | Geo       | Metro  | 1998 | 2015-12-18 00:00:00 | NULL       |
 +--------+-----------+-----------+--------+------+---------------------+------------+
 5 rows in set (0.00 sec)
下面是带有模型和查询执行的实际NodeJS脚本:

//app.js
"严格使用",;
const Config=require('./Config');
const Bookshelf=需要('./书架');
var Driver=Bookshelf.Model.extend({
tableName:'驱动程序',
汽车:功能(){
还这个。有很多(车);
}
});
var Car=Bookshelf.Model.extend({
表名:“汽车”,
驱动程序:函数(){
将此.belongsTo(驾驶员)返回;
}
});
新司机()
.在哪里({
司机编号:2
})
.取回({
与相关:[“汽车”]
})
.then(功能(驱动程序){
log('relatedcar:',JSON.stringify(driver));
console.log('驱动程序数据',驱动程序);
});
此外,这里还有bookshelf.js文件,其中包含KnexJS和BookshelfJS连接:

//bookshelf.js
"严格使用",;
var knex=require('knex')(require('./config')。数据库);
module.exports=require('bookshelf')(knex);
这是执行app.js时的控制台输出

RELATED CAR: {"driver_id":2,"name":"The Stig","created_at":"2015-12-08T07:00:00.000Z","updated_at":null,"cars":[]}
 DRIVER DATA ModelBase {
  attributes:
   { driver_id: 2,
     name: 'The Stig',
     created_at: Tue Dec 08 2015 00:00:00 GMT-0700 (MST),
     updated_at: null },
  _previousAttributes:
   { driver_id: 2,
     name: 'The Stig',
     created_at: Tue Dec 08 2015 00:00:00 GMT-0700 (MST),
     updated_at: null },
  changed: {},
  relations:
   { cars:
      CollectionBase {
        model: [Object],
        length: 0,
        models: [],
        _byId: {},
        relatedData: [Object] } },
  cid: 'c1',
  _knex: null }
我不确定问题是什么,我有一种感觉,我忽略了一件相当简单的事情

谢谢

有两个问题

  • cars.driver\u id
    需要取消签名,就像它在drivers表中引用的id列一样
  • 我将表索引列创建为
    ${singular\u table\u name}\u id
    ,根据我的经验,这是最常见的方法,但BookshelfJS希望它只是
    id
    。因此,我可以将ID列名更改为just
    ID
    ,或者将
    idAttribute
    值设置为
    ${singular\u table\u name}\u ID
  • 上述更改后,一切正常。

    有两个问题

  • cars.driver\u id
    需要取消签名,就像它在drivers表中引用的id列一样
  • 我将表索引列创建为
    ${singular\u table\u name}\u id
    ,根据我的经验,这是最常见的方法,但BookshelfJS希望它只是
    id
    。因此,我可以将ID列名更改为just
    ID
    ,或者将
    idAttribute
    值设置为
    ${singular\u table\u name}\u ID
  • 以上更改后一切正常