[英]How can I join three tables in Bookshelf.js?
我阅读了与through
相关的书架文档,但是,我找不到应该如何进行的工作。 我有三个表,其名称与书架使用的约定不同。 基本上,一个组通过配置文件有许多用户。 建立连接的最后一个。
Table Name: User
- id_user
- username
- password
Table Name: Profile
- id_user
- id_group
Table Name: Group
- id_group
- name
- description
- status
我的小组模型如下:
module.export = BookShelf.model('Group', {
tableName: 'pats_grupos',
users: function() {
return this.hasMany('User').through('Profile');
}
});
考虑到我的表不遵循_id
约定(而是id_
一个),我如何告诉Bookshelf使用我的自定义表命名模式?
根据Bookshelf的idAttribute
文档 ,当不使用默认的'id'时,必须更改模型以显式声明所使用的id属性。 喜欢:
module.export = BookShelf.model('Group', {
tableName: 'pats_grupos',
idAttribute: 'id_group',
users: function() {
return this.hasMany('User').through('Profile');
}
});
并且由于您的外键也没有遵循书架默认的命名方式,因此您可能也必须在through()
调用中明确声明它们。 就像是:
//...
users: function() {
return this
.hasMany('User')
.through('Profile', 'id_user', 'id_group');
}
//...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.