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?