如何在环回 3 中正确设置 belongsTo 关系?

how to set up correctly belongsTo relation on looback 3?

我在环回 3 上遇到 belongsTo 关系问题, 这是一对一的关系,一个 clientParticipation 有一个 kartParticipation 这是我对每个型号的配置

KartParticipation.json

"ClientParticipation": {
      "type": "belongsTo",
      "model": "ClientParticipation",
      "foreignKey": "kartParticipationId"
    },

ClientParticipation.json

"KartParticipation": {
      "type": "belongsTo",
      "model": "KartParticipation",
      "foreignKey": "clientParticipationId"
    },

当我尝试像

那样保存我的关系时
kartParticipation.ClientParticipation(clientParticipation);

我可以看到

console.log(clientParticipation)

我的关系已正确完成但未存储在数据库中

有人可以解释一下我做错了什么吗?

也许我错了,但是两个关系 belongsTo 对我来说毫无意义... 这就是我要做的:

KartParticipation.json中:

"clientParticipation": {  //starts the included relation with a lowerCase might be best practice, tho
      "type": "belongsTo",
      "model": "ClientParticipation",
      "foreignKey": "" //you don't need to precise a custom foreign if you use the classic one (easiest to read, imo)
    }

ClientParticipation.json中:

"kartParticipation": {
      "type": "hasOne",
      "model": "KartParticipation",
      "foreignKey": ""
    }

然后,添加关系:

ClientParticipation.findOne({where: {id: 'your_id'}}, function (err, clientParticipation) {
                if (err)
                    return cb(err, null);

                if (clientParticipation && clientParticipation.id) {
                    var newKartParticipation = {'object_with': 'your_datas'};
                    clientParticipation.kartParticipation.add(newKartParticipation); // this is the code you were seeking, tho
                }
            });

最后,您可以通过以下请求检查关系是否存在:

GET /ClientParticipation/{id}/kartParticipation

或以编程方式:

ClientParticipation.findOne({where: {id: 'your_id'}, include : 'kartParticipation'}, function (err, clientParticipation) {
    console.log(clientParticipation.kartParticipation);
});