双方是否应该建立一对一的联系? [英] Should one-to-one associations populate both sides?
问题描述
我有两个模型(用户和代理).然后,我创建一个用户和一个代理.我希望在同时为/user和/agent使用蓝图路由时看到关联.我只看到用户模型通过/agent蓝图与代理相关联. /user蓝图与代理没有任何引用/关联.
I have two models (user & agent). I then create a user and an agent. I am expecting to see the association when using the blueprint routes for BOTH /user and /agent. I am only seeing the user model is associated with an agent via the /agent blueprint. The /user blueprint does not have any reference/association to an Agent.
当我尝试使用以下命令通过A userId访问代理时,就会出现此问题:
The issue presents itself when I am trying to access the Agent via A userId with the following command:
User.findOne(req.body.userId).populate('agent').exec(function(err, agent)
代理"实际上是用户信息,而不是代理.
"agent" is in fact the user information...not the agent.
这是我的模特:
用户:
attributes: {
agent: {
model: 'agent',
via: 'owner'
}
}
代理商:
attributes: {
owner: {
model: 'user'
}
}
感谢阅读!
推荐答案
Sails不能完全支持一对一的模型关联-您必须在两侧都设置外键".有关更多信息,请参见 https://stackoverflow.com/a/27752329/345484 .我很想把这个问题作为那个问题的副本来解决,但设置略有不同.
Sails does not fully support one-to-one model associations--you have to set the "foreign key" on both sides. See https://stackoverflow.com/a/27752329/345484 for more information. I'm tempted to just close this question as a duplicate of that one, but the setup is slightly different.
这篇关于双方是否应该建立一对一的联系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!