在相关模型中包含子模型
我有帐户模式,其中有许多post,和post属于帐户
帐户:
{ "name": "Account", "base": "User", "relations": { "post": { "type": "hasMany", "model": "Post", "foreignKey": "accountId" }, ... }, ... }
post:
{ "name": "Post", "base": "PersistedModel", "relations": { "account": { "type": "belongsTo", "model": "Account", "foreignKey": "" } }, ... }
现在,我有模型问题,这是Post模型的子模型。
{ "name": "Question", "base": "Post", ... }
我想查询特定的帐户所有的领域,并包括他所有的问题,这样的事情
Account.findById({ id: id, filter: {include: 'question'}, function(){...});
可能吗?
Account.findById(id, { include: { relation: 'questions' } }, function(){...});
您可能需要在Account
模型中创buildquestions
关系,因为我不认为它会inheritancePost
模型中的questions
关系。
还要注意,你应该把post
关系重命名为posts
。 所以你的关系部分应该是这样的:
Account: { "name": "Account", "base": "User", "relations": { "posts": { "type": "hasMany", "model": "Post", "foreignKey": "accountId" }, "questions": { "type": "hasMany", "model": "Question", "foreignKey": "accountId" } ... }, ... }