问题
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