Reflexive协会如何在SAILS JS中工作?

时间:2018-06-12 09:27:49

标签: javascript mysql sails.js

这是我的用户模型:我试图在用户之间创建一个反身关联。导师和受指导者

fullName: {
  type: 'string',
  required: true,
  description: 'Full representation of the user\'s name',
  maxLength: 120,
  example: 'Lisa Microwave van der Jenny'
},

typeOfUser: {
  required: true,
  type: 'string',
  example: 'mentor',
  description: 'The user\'s type of account.',
},
skills:{
  collection:'skills',
  via: 'userId'
},

mentees:{
  collection: 'user',
  via: 'mentors'
},
mentors:{
  collection:'user',
  via: 'mentees'
}

这是我的代码。

// Add mentor of mentorId as the mentor of currentUser
await User.addToCollection(3, 'mentor',8);

// Find mentor and populate mentees 
var showMentee = await User.findOne(8).populate('mentee');

回应是正确的:

{
    "mentee": [
        {
            "id": 3,
            "fullName": "1234",
            "typeOfUser": "mentee"
        }
    ],
    "id": 8,
    "fullName": "1234",
    "typeOfUser": "mentor"
}

但问题在于帆创建的关联表:

+----+-------------+-------------+
| id | user_mentee | user_mentor |
+----+-------------+-------------+
|  1 |           8 |           3 |
|  2 |           8 |           3 |
+----+-------------+-------------+

在这种情况下,我无法理解列命名。导师身份是8,被指导者身份是3,但是显示相反。

0 个答案:

没有答案