Skip to content

Instantly share code, notes, and snippets.

@martinaglv
Created May 16, 2014 08:50
Show Gist options
  • Save martinaglv/882494d4347c844eb7c4 to your computer and use it in GitHub Desktop.
Save martinaglv/882494d4347c844eb7c4 to your computer and use it in GitHub Desktop.
Bookshelf belongsTo
-- --------------------------------------------------------
--
-- Table structure for table `teams`
--
CREATE TABLE IF NOT EXISTS `teams` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `teams`
--
INSERT INTO `teams` (`id`, `name`) VALUES
(1, 'Marketing'),
(2, 'Sales');
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`team_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `team_id`) VALUES
(1, 1),
(2, 2);
Possibly unhandled Error: ER_BAD_FIELD_ERROR: Unknown column 'team_id' in 'field list', sql: update `teams` set `id` = ?, `name` = ?, `team_id` = ? where `id` = ?, bindings: 1,Developers,1,1
at /home/martin/node_modules/bookshelf/node_modules/knex/clients/server/base.js:73:22
at tryCatch1 (/home/martin/node_modules/bluebird/js/main/util.js:63:19)
at Promise$_callHandler [as _callHandler] (/home/martin/node_modules/bluebird/js/main/promise.js:695:13)
at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/martin/node_modules/bluebird/js/main/promise.js:711:18)
at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/martin/node_modules/bluebird/js/main/promise.js:868:14)
at Promise$_settlePromises [as _settlePromises] (/home/martin/node_modules/bluebird/js/main/promise.js:1006:14)
at Promise$_rejectPromises [as _rejectPromises] (/home/martin/node_modules/bluebird/js/main/promise.js:999:10)
at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/martin/node_modules/bluebird/js/main/async.js:74:12)
at Async$consumeFunctionBuffer (/home/martin/node_modules/bluebird/js/main/async.js:37:14)
at process._tickCallback (node.js:419:13)
var Bookshelf = require('bookshelf');
var MySql = Bookshelf.initialize({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'root',
password : '',
database : 'testdb',
charset : 'utf8'
}
});
var Team = MySql.Model.extend({
tableName: 'teams',
users: function(){
return this.hasMany(User);
}
});
var User = MySql.Model.extend({
tableName: 'users',
team: function(){
return this.belongsTo(Team);
}
});
new User({id:1}).fetch().then(function(user){
user.team().fetch().then(function(team){
// This attempts to set team_id=1 on the teams table
team.set({name: 'Developers'}).save().then(function(t){
console.log(t);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment