Skip to content

Instantly share code, notes, and snippets.

@tgriesser
Forked from stefanhenze/gist:9821183
Last active August 29, 2015 13:57
Show Gist options
  • Save tgriesser/9822343 to your computer and use it in GitHub Desktop.
Save tgriesser/9822343 to your computer and use it in GitHub Desktop.
var Parent = Bookshelf.Model.extend({
tableName: 'parent',
child: function() {
return this.hasOne(Child);
}
});
var Child = Bookshelf.Model.extend({
tableName: 'child',
//this has an attribute parent_id
parent: function() {
return this.belongsTo(Parent);
}
});
Parent.forge({name: "Parent"}).save()
.then(function (parent) {
// or parent.child().save({name: 'Child'})
// using .related just throws it on the
// parent's .relations hash
return parent.related('child').save({name: 'Child'});
})
.then(function (child) {
res.send(child);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment