[英]How can I join three tables in Bookshelf.js?
I read the Bookshelf documentation related to through
, but, but I can't found out how I should proceed. 我阅读了与through
相关的书架文档,但是,我找不到应该如何进行的工作。 I have three tables named in a different convention than Bookshelf uses. 我有三个表,其名称与书架使用的约定不同。 Basically, a Group has many Users through Profile. 基本上,一个组通过配置文件有许多用户。 The last one that makes the connection. 建立连接的最后一个。
Table Name: User
- id_user
- username
- password
Table Name: Profile
- id_user
- id_group
Table Name: Group
- id_group
- name
- description
- status
My group model is like so: 我的小组模型如下:
module.export = BookShelf.model('Group', {
tableName: 'pats_grupos',
users: function() {
return this.hasMany('User').through('Profile');
}
});
Taking in consideration that my tables don't follow the _id
convention (but instead, the id_
one), how can I tell Bookshelf to work with my custom table naming pattern? 考虑到我的表不遵循_id
约定(而是id_
一个),我如何告诉Bookshelf使用我的自定义表命名模式?
Accordingly to Bookshelf's idAttribute
documentation , when not using the default 'id' you must change your models to explicitly declare the id attribute used. 根据Bookshelf的idAttribute
文档 ,当不使用默认的'id'时,必须更改模型以显式声明所使用的id属性。 Like: 喜欢:
module.export = BookShelf.model('Group', {
tableName: 'pats_grupos',
idAttribute: 'id_group',
users: function() {
return this.hasMany('User').through('Profile');
}
});
And since your foreign keys are also not following Bookshelf default naming you may have to declare them explicitly on the through()
call too. 并且由于您的外键也没有遵循书架默认的命名方式,因此您可能也必须在through()
调用中明确声明它们。 Something like: 就像是:
//...
users: function() {
return this
.hasMany('User')
.through('Profile', 'id_user', 'id_group');
}
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.