书架 hasMany().through() 参数被忽略
Bookshelf hasMany().through() argument ignored
我有一个非常传统的 Course
模型。我还有一个 Book
模型,但不是。在他们之间,我有一个 CourseMaterial
模型。
我正在尝试配置我的 Course
模型 Course.hasMany('Book').through('CourseMaterial')
,但由于一些非常规的东西,我需要指定每个键。 table 的加入方式如下:
-
courses
table 有一个 id
。
courseMaterials
table 作为 FK,course_id
引用 courses.id
。
course_materials
table 有一个 identifier
字段,为了讨论的目的,它包含一个 ISBN 值。
books
table 有一个 isbn
table 其值将匹配 course_materials.identifier
. 中的值
在我的 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')
我有一个非常传统的 Course
模型。我还有一个 Book
模型,但不是。在他们之间,我有一个 CourseMaterial
模型。
我正在尝试配置我的 Course
模型 Course.hasMany('Book').through('CourseMaterial')
,但由于一些非常规的东西,我需要指定每个键。 table 的加入方式如下:
-
courses
table 有一个id
。 courseMaterials
table 作为 FK,course_id
引用courses.id
。course_materials
table 有一个identifier
字段,为了讨论的目的,它包含一个 ISBN 值。books
table 有一个isbn
table 其值将匹配course_materials.identifier
. 中的值
在我的 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')