One to Many relationship in CompoundJS One to Many relationship in CompoundJS express express

One to Many relationship in CompoundJS


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