node.js - model.fetch with related models bookshelfjs -


i have below models

company.js

var company = db.model.extend({   tablename: 'company',   hastimestamps: true,   hastimestamps: ['created_at', 'updated_at'] }); 

user.js

var user = db.model.extend({   tablename: 'user',   hastimestamps: true,   hastimestamps: ['created_at', 'updated_at'],   companies: function() {     return this.belongstomany(company);   } }); 

with many-to-many relation between company , user handle via following table in database.

user_company.js

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);   } }); 

the problem when run following query.

var user = new user({ id: req.params.id }); user.fetch({withrelated: ['companies']}).then(function( user ) {   console.log(user); }).catch(function( error ) {   console.log(error); }); 

it logs following error because looking company_user table instead of user_company.

{ [error: select `company`.*, `company_user`.`user_id` `_pivot_user_id`, `company_user`.`company_id` `_pivot_company_id` `company` inner join `company_user` on `company_user`.`company_id` = `company`.`id` `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 } 

is there way tell table while fetching relations?

with bookshelf.js important, how tables , ids named in database. bookshelf.js interesting things foreign keys (i.e. converts singular , appends _id).

when using bookshelfjs's many-to-many feature, don't need usercompany model. however, need following naming conventions of tables , ids work.

here's example of many-to-many models. firstly, database:

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');   }); }; 

please note how junction table named: alphabetically ordered (authors_books). if you'd write books_authors, many-to-many features wouldn't work out of box (you'd have specify table name explicitly in model). note foreign keys (singular of authors _id appended, i.e. author_id).

now let's @ models.

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);   } }); 

now our database has correct naming of tables , ids, can use belongstomany , works! there no need authorbook model, bookshelf.js you!

here's advanced description: http://bookshelfjs.org/#model-instance-belongstomany


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -