【问题标题】:One to Many relationship in CompoundJSCompoundJS 中的一对多关系
【发布时间】:2013-02-19 21:41:22
【问题描述】:

我是 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);
});

我将关系放在models/Book.js 中。 这是我的Book.js(models/Book.js):

module.exports = function (compound, Book) {
  Book.hasMany(compound.models.Author,   {as: 'author',  foreignKey: 'authorId'});
};

这是我的Author.js (models/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'});

但结果是一样的。

上面的代码有问题吗?如果是这样,我该如何解决?

【问题讨论】:

标签: node.js express railway.js compoundjs


【解决方案1】:

compoundjs 的作者似乎没有实现 Model 功能。现在你的关系应该定义在你的架构文件的末尾。

此外,您通过存储定义函数的返回值来覆盖方案对象。删除 var Book = 和 var Author =。

而且,foreignKey 是自动创建的。

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'});

更新:

哦。您的问题不是定义关系,而是使用它们。 jugglingdb 的文档对此不是很清楚。为了建立关系,您必须使用以下格式:有关详细信息,请参阅 DOCS: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()
})

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()
})

【讨论】:

  • 我从模型 Book.js 和 Author.js 中删除关系并将关系放入 schema.js 本身,但结果是相同的。
  • 结果还是一样,让我解释一下我的场景。我的场景是:我在书籍模型标题 isbn authorId 中有三个字段。在作者模型中我有一个字段作为名称。我必须输入如果书籍模型中的 authorId 存在于作者模型中,则为一本书。因此书籍模型中的 auhorId 是指作者模型的主键(主键“id”将在迁移时自动生成)
  • 有没有办法使用复合中的迁移选项来实现这个?
  • 好吧,它只会在你的控制器中有用。
  • 有没有办法通过使用复合数据库迁移在 MySQL 表本身中创建外键。否则,如果我在我的 MySQL 表中手动创建外键,一个问题是在复合 db 迁移||更新期间,它将删除我在 MySQL 表中手动设置的外键关系。我该如何解决这个问题?
猜你喜欢
  • 1970-01-01
  • 2018-06-15
  • 2018-09-27
  • 2011-03-30
  • 2023-01-16
  • 2015-09-20
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多