当运动员和使用 Sequelize 的组织之间有多个表时,我希望能够检查运动员是否属于一个组织。这可能吗?或者我需要将协会扩展到运动员班级吗?
正如你所看到的,一个运动员属于一个团队,属于一个部门等等......直到一个组织。
I have a node/mysql database running using Sequelize. I have used ruby on rails before, where you can use a "through" to indicate that an indirect relationship can be found through a particular direct relationship, but I haven't found anything like that for Sequelize on a belongsTo relationship.
Just thinking this through, even if there is a solution to do this, I'm guessing that sequelize will be joining all of these tables, which would be expensive. Would it be best to perhaps just have the organization associated directly on the athlete instead of linked through a series of other classes?
Thank you for your help.
In Sequelize, you can have nested belongsTo
's in a query. In your case it'd look like:
Athlete.find({
include: [
{
model: Team,
include: [
model: Division,
include: [
model: AgeGroup,
include: [
model: League,
include: [
model: Season,
include: [
model: Organisation,
]
]
]
]
]
}
]
});
这很丑陋 - 但不一定是问题。首先要做的是尝试一下,看看性能是否有问题 - 很有可能它会很好。
如果性能是一个问题,并且您已经准备好所有数据库索引和内容,但仍然无法获得合理的性能,那么是的,organisation_id
直接在运动员上添加是可以的。这称为“非规范化” - 您可以在其中存储冗余数据以优化读取性能。这是您应该尽可能避免的事情 - 但是当您无法避免时,这很好。
你一定要记住,当你改变一个运动员team_id
,你也必须更新自己的organisation_id
了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句