复合JS中的一对多关系 [英] One to Many relationship in CompoundJS

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

问题描述

我是CompoundJS的新手,而且我在jugglingDB中建立一个很多关系的问题。我使用MySQL作为数据库。

I'm new to CompoundJS and I had a problem in setting up a one to many relationsip with jugglingDB.I'm using MySQL as database.

我有设置两本书和作者模型。

I have set up two model Book and Author.

书籍有很多作者。

这是我的 schema.js (db / schema.js):

This is my schema.js (db/schema.js):

var Book = describe('Book', function () {
    property('title', String);
    property('isbn', String);
    property('authorId', Number);
    set('restPath', pathTo.books);
});

var Author = describe('Author', function () {
    property('name', String);
    property('authorId', Number);
    set('restPath', pathTo.authors);
});

我将关系放在模型/ Book.js中。
这是我的 Book.js (models / Book.js):

I put the relationship in the models/Book.js. This is my Book.js (models/Book.js):

module.exports = function (compound, Book) {
  Book.hasMany(compound.models.Author,   {as: 'author',  foreignKey: 'authorId'});
};

这是我的 Author.js /Author.js):

This is my Author.js (models/Author.js):

module.exports = function (compound, Author) {
 Author.belongsTo(compound.models.Book, {as: 'books', foreignKey: 'authorId'});
};

问题是我无法创建这些关系。当我检查表中没有外键设置在表中。

The problem is that I can't create these relations. When I check the table no foreign key set in the table.

我从模型Book.js和Author.js中删除关系,并将关系放在模式中。 js本身

I remove the relation from the models Book.js and Author.js and put the relation in the schema.js itself

之后,schema.js如下所示:

After that the schema.js look like this:

var Book = describe('Book', function () {
    property('title', String);
    property('isbn', String);
    property('authorId', Number);
    set('restPath', pathTo.books);
});

var Author = describe('Author', function () {
    property('name', String);
    property('authorId', Number);
    set('restPath', pathTo.authors);
});

Book.hasMany(Author, {as: 'author',  foreignKey: 'authorId'});
Author.belongsTo(Book, {as: 'books', foreignKey: 'authorId'});

但结果是一样的。

上面的代码有什么问题吗?如果是这样,我可以解决这个问题吗?

Is there any problem in the above code ? if so how can I solve that?

推荐答案

/ strike>现在,您的关系应该在模式文件的末尾定义。

It seems that the Author of compoundjs has not implemented Model functionality. For now your relationships should be defined at the end of your schema file.

此外,您通过存储定义函数的返回值来覆盖schemea对象。删除var Book =和var Author =。

Also, you are overriding the schemea objects by storing the return value of the define function. Remove var Book = and var Author =.

而且,外键自动创建。

schema.js :

schema.js:

describe('Book', function () {
    property('title', String);
    property('isbn', String);
    set('restPath', pathTo.books);
});

describe('Author', function () {
    property('name', String);
    set('restPath', pathTo.authors);
});

Book.hasMany(Author, {as: 'author',  foreignKey: 'authorId'});
Author.belongsTo(Book, {as: 'books', foreignKey: 'authorId'});

更新:

你的问题不是定义关系,而是使用它们。
jugglingdb的文档不是很清楚。为了建立关系,您必须使用以下格式:有关详细信息,请参阅DOCS: https:// github。 com / 1602 / jugglingdb

OH. Your problem is NOT defining the relationships, but using them. jugglingdb's docs are not very clear on this. In order to establish a relationship, you must use the following format: See the DOCS for more info: https://github.com/1602/jugglingdb

Author.find(id_here_as_string, function(err, author_record){
  book_record = new Book({
    title: 'whatever'
    isbn: 'again whatever here'
  });
  book_record.author(author_record);
  book_record.save()
})

Author.find(id_here_as_string, function(err, author_record){
  book_record = author_record.books.build({
    title: 'whatever'
    isbn: 'again whatever here'
  });
  book_record.save()
})

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

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