书架 hasMany().through() 参数被忽略

Bookshelf hasMany().through() argument ignored

我有一个非常传统的 Course 模型。我还有一个 Book 模型,但不是。在他们之间,我有一个 CourseMaterial 模型。

我正在尝试配置我的 Course 模型 Course.hasMany('Book').through('CourseMaterial'),但由于一些非常规的东西,我需要指定每个键。 table 的加入方式如下:

在我的 Course 模型中,我的关系如下所示:

books: function() {
    return this.hasMany('Book')
        .through('CourseMaterial', 'isbn', 'identifier');
},

生成的SQL好像忽略了第三个参数:

SELECT `books`.*, 
       `course_materials`.`id` AS `_pivot_id`, 
       `course_materials`.`course_id` AS `_pivot_course_id` 
  FROM `books` 
        INNER JOIN `course_materials` 
                ON `course_materials`.`id` = `books`.`isbn`

为什么尝试加入 course_materials.id 而不是 course_materials.identifier?我是不是误读了文档?

这是一个存在的问题,如下所示 link:

https://github.com/tgriesser/bookshelf/issues/673

所以书架图书馆忽略了第三个参数。

解决方案是使用 knex 构建自定义 SQL 作为定义每个步骤的方式,如下所示:

knex('books').innerJoin('CourseMaterial', 'CourseMaterial.identifier', 'books.isbn')

OR (in case you want to do projection on results)

knex('books').select(['books.id', 'books.bookname'])
.innerJoin('CourseMaterial', 'CourseMaterial.identifier', 'books.isbn')