Bookshelf.js save one to many relation

老子叫甜甜 提交于 2020-01-06 14:45:40

问题


I am trying to save a one to many relation My models are

Foo

foo = bookshelf.Model.extend({
        tableName: 'foo',
        bar: function () {
            return this.hasMany('bar', 'barId');
        }

Bar

bar = bookshelf.Model.extend({
        tableName: 'bar',
        foo: function () {
            return this.belongsTo('foo', 'barId');
        }

What I am trying to do

var Foo = { id: 7 }
new Bar({ blah: blah.val  })
                .foo()
                .attach(foo);

The error I am getting

(intermediate value).foo().attach is not a function

Any help will be appreciated.


回答1:


I don't think you can save data like that. I assume that the table bar has foo_id column, so all you need to do is

new Bar().save({ blah: blah.val, foo_id: Foo.id}).then(function(){});



来源:https://stackoverflow.com/questions/36096072/bookshelf-js-save-one-to-many-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!