Bookshelf.js保存一对多关系 [英] Bookshelf.js save one to many relation

查看:124
本文介绍了Bookshelf.js保存一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存一对多的关系 我的模特是

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 = bookshelf.Model.extend({
        tableName: 'bar',
        foo: function () {
            return this.belongsTo('foo', 'barId');
        }

我要做什么

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

我遇到的错误

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

任何帮助将不胜感激.

Any help will be appreciated.

推荐答案

我认为您无法保存这样的数据.我假设表bar具有foo_id列,所以您要做的就是

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

这篇关于Bookshelf.js保存一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆