I am new to CompoundJS, and I had a problem setting up a one-to-many relationship with jugglingDB. I use MySQL as a database.
I created two models of the book and the author.
There are many authors in the book.
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 have established relationships in /Book.js models. This is mine Book.js(models / Book.js):
module.exports = function (compound, Book) {
Book.hasMany(compound.models.Author, {as: 'author', foreignKey: 'authorId'});
};
This is mine Author.js(models / Author.js):
module.exports = function (compound, Author) {
Author.belongsTo(compound.models.Book, {as: 'books', foreignKey: 'authorId'});
};
The problem is that I cannot create this relationship. When I check the table, the foreign key will not be set in the table.
I remove the relation from the Book.js and Author.js models and put the relation in schema.js itself
After that, schema.js looks 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 the same.
Are there any problems in the above code? if so, how can I solve this?