5

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?

Jetson John
  • 3,759
  • 8
  • 39
  • 53

1 Answers1

4

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()
})
legomind
  • 241
  • 1
  • 2
  • 7
  • I remove the relation from the models Book.js and Author.js and put the relation in the schema.js itself but the result is same. – Jetson John Mar 07 '13 at 05:16
  • 1
    Still the result is the same let me explain my scenario.My scenario is: I have three fields in book model title isbn authorId .and in author model I have one fields as name.I have to enter a book if the authorId in the book model is present in author model.so the auhorId in the book model is referring to the primary key of the author model (primary key 'id' will auto generated on migrating) – Jetson John Mar 08 '13 at 10:38
  • Is there any way to implement this using migrate option in compound ? – Jetson John Mar 11 '13 at 06:40
  • well it would only be useful in your controller. – legomind Mar 12 '13 at 14:21
  • Is there any way to create the foreign key in the MySQL table itself by using compound db migrate. Otherwise if I create foreign key manually in my MySQL table one problem is that during compound db migrate||update it will drop the foreign key relation I set manually in the MySQL table. How can I solve this problem? – Jetson John Mar 14 '13 at 07:38