One to Many relationship in CompoundJS

老子叫甜甜 提交于 2019-11-28 01:34:33

问题


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.

Book has many authors.

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

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'});
};

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.

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

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'});

but the result is same.

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


回答1:


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.

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

And, the foreignKey is created automatically.

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'});

Update:

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

OR

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


来源:https://stackoverflow.com/questions/15224642/one-to-many-relationship-in-compoundjs

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