我自己有多种型号:
用户(id,用户名)
团队(身份证,姓名)
技能(身份证,姓名)
TeamSkill(id,skill_id,team_id)
TeamSkillUpvote(id,team_skill_id,user_id)
我想提出这样的问题:
select team.id, team.name, team_skill_upvotes.user_id
from skill, team_skills, team
where skill.id = <THE_ID_OF_MY_MODEL>
and skill.id = team_skills.skill_id
and team_skills.team_id = team.id
and team_skills.id = team_skill_upvotes.team_skill_id
任何人都可以提供帮助吗?
谢谢!
答案 0 :(得分:0)
搜索后我找到了答案......它只需几步即可完成:
1:定义TeamSkill的关系:
export interface ITeamSkillRelations {
team: Team;
upvotes: Collection<TeamSkillUpvote>;
}
export class TeamSkill extends bookshelf.Model<TeamSkill>{
...
public relations: ITeamSkillRelations;
public upvotes(): Collection<TeamSkillUpvote> {
return this.hasMany(TeamSkillUpvote, TeamSkillUpvote.teamSkillIdAttribute/*team_skill_id*/);
}
public team(): Team {
return this.belongsTo(Team, TeamSkill.teamIdAttribute/*team_id*/);
}
...
}
2:神奇:
export class Skill extends bookshelf.Model<Skill> {
public getTeams(): Promise<ITeamOfASkill[]> {
var fetchOptions: CollectionFetchOptions = {
withRelated: [
TeamSkill.relatedTeamSkillUpvotesAttribute/*upvotes*/,
TeamSkill.relatedTeamAttribute/*team*/
]
};
return this.teamSkills()
.fetch(fetchOptions)
.then((teamSkillsCollection: Collection<TeamSkill>) => {
var teamSkills: TeamSkill[] = teamSkillsCollection.toArray();
return _.map(teamSkills, _skill => this._convertTeamSkillToTeamOfASkill(_skill));
});
}
private _convertTeamSkillToTeamOfASkill(teamSkill: TeamSkill): ITeamOfASkill {
var team: Team = teamSkill.relations.team;
var upvotesCollection: Collection<TeamSkillUpvote> = teamSkill.relations.upvotes;
var upvotes: TeamSkillUpvote[] = upvotesCollection.toArray();
var upvotingIds =
_.map(upvotes, _ => _.attributes.user_id);
return {
team: team,
upvotingUserIds: upvotingIds
};
}
}