Node.js model.fetch与相关模型书架FJS
我有以下型号 company.jsNode.js model.fetch与相关模型书架FJS,node.js,bookshelf.js,Node.js,Bookshelf.js,我有以下型号 company.js var Company = DB.Model.extend({ tableName: 'company', hasTimestamps: true, hasTimestamps: ['created_at', 'updated_at'] }); var UserCompany = DB.Model.extend({ tableName: 'user_company', hasTimestamps: true, hasTimestamp
var Company = DB.Model.extend({
tableName: 'company',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at']
});
var UserCompany = DB.Model.extend({
tableName: 'user_company',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at'],
users: function() {
return this.belongsToMany(User);
},
companies: function() {
return this.belongsToMany(Company);
}
});
user.js
var User = DB.Model.extend({
tableName: 'user',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at'],
companies: function() {
return this.belongsToMany(Company);
}
});
在公司
和用户
之间具有多对多
关系,通过数据库中的下表进行处理
user_company.js
var Company = DB.Model.extend({
tableName: 'company',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at']
});
var UserCompany = DB.Model.extend({
tableName: 'user_company',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at'],
users: function() {
return this.belongsToMany(User);
},
companies: function() {
return this.belongsToMany(Company);
}
});
问题是当我运行以下查询时
var user = new User({ id: req.params.id });
user.fetch({withRelated: ['companies']}).then(function( user ) {
console.log(user);
}).catch(function( error ) {
console.log(error);
});
它记录以下错误,因为它正在查找company\u user
表,而不是user\u company
{ [Error: select `company`.*, `company_user`.`user_id` as `_pivot_user_id`, `company_user`.`company_id` as `_pivot_company_id` from `company` inner join `company_user` on `company_user`.`company_id` = `company`.`id` where `company_user`.`user_id` in (2) - ER_NO_SUCH_TABLE: Table 'navardeboon.company_user' doesn't exist]
code: 'ER_NO_SUCH_TABLE',
errno: 1146,
sqlState: '42S02',
index: 0 }
有没有办法告诉它在获取关系时查找某个表?对于Bookshelf.js,表和ID在数据库中的命名方式非常重要。Bookshelf.js使用外键做了一些有趣的事情(即将其转换为单数并附加
\u id
)
使用BookshelfFJS的多对多功能时,您不需要UserCompany
model。但是,您需要遵循表和ID的命名约定才能工作
这是一个多对多模型的示例。首先,数据库:
exports.up = function(knex, Promise) {
return knex.schema.createTable('books', function(table) {
table.increments('id').primary();
table.string('name');
}).createTable('authors', function(table) {
table.increments('id').primary();
table.string('name');
}).createTable('authors_books', function(table) {
table.integer('author_id').references('authors.id');
table.integer('book_id').references('books.id');
});
};
请注意连接表的命名方式:按字母顺序(authors\u books
)。如果您要编写书籍\u authors
,多对多功能将无法立即使用(您必须在模型中明确指定表名)。还要注意外键(附加了\u id
的作者的单数,即作者id)
现在让我们看看模型
var Book = bookshelf.Model.extend({
tableName: 'books',
authors: function() {
return this.belongsToMany(Author);
}
});
var Author = bookshelf.Model.extend({
tableName: 'authors',
books: function() {
return this.belongsToMany(Book);
}
});
既然我们的数据库已经正确命名了表和ID,我们就可以使用belongtomany了,这样就行了!不需要使用AuthorBook
model,Bookshelf.js可以为您实现这一点
下面是高级描述:实际上我找到了一个非常简单的解决方案。您只需要像这样提及表名:
var User = DB.Model.extend({
tableName: 'user',
hasTimestamps: true,
hasTimestamps: ['created_at', 'updated_at'],
companies: function() {
return this.belongsToMany(Company, **'user_company'**);
}
})
正如@uglycode所说,不再需要UserCompany
模型