我是 CompoundJS 的新手,在与 jugglingDB 建立一对多关系时遇到了问题。我使用 MySQL 作为数据库。
我已经设置了两个模型书和作者。
本书有许多作者。
这是我的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);
});
我将关系放在模型/Book.js 中。这是我的Book.js
(模型/Book.js):
module.exports = function (compound, Book) {
Book.hasMany(compound.models.Author, {as: 'author', foreignKey: 'authorId'});
};
这是我的Author.js
(模型/Author.js):
module.exports = function (compound, Author) {
Author.belongsTo(compound.models.Book, {as: 'books', foreignKey: 'authorId'});
};
问题是我无法创建这些关系。当我检查表时,表中没有设置外键。
我从模型 Book.js 和 Author.js 中删除关系并将关系放入 schema.js 本身
之后 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);
});
Book.hasMany(Author, {as: 'author', foreignKey: 'authorId'});
Author.belongsTo(Book, {as: 'books', foreignKey: 'authorId'});
但结果是一样的。
上面的代码有什么问题吗?如果是这样,我该如何解决?