unrelate()似乎没有更改或删除联接表

时间:2019-04-27 16:27:43

标签: node.js knex.js objection.js

UserTagAnswer模型:

  answer: {
    relation: Model.ManyToManyRelation,
    modelClass: sails.config.models.UserTagAnswerOptions,
    join: {
      from: 'usertaganswer.id',
      through: {
        from: 'usertaganswer_answeredoptions.userTagAnswerId',
        to: 'usertaganswer_answeredoptions.userTagAnswerOptionId'
      },
      to: 'usertagansweroptions.id'
    }
  },

UserTagAnswerOptions模型

  userTagAnswers: {
    relation: Model.ManyToManyRelation,
    modelClass: sails.config.models.UserTagAnswer,
    join: {
      from: 'usertagansweroptions.id',
      through: {
        from: 'usertaganswer_answeredoptions.userTagAnswerOptionId',
        to: 'usertaganswer_answeredoptions.userTagAnswerId'
      },
      to: 'usertaganswer.id'
    }
  },

我在数据库中看到联接表usertaganswer_answeredoptions中有一行包含所有期望的信息。但我现在想删除该行。我尝试这样做:

theAnswer = await UserTagAnswer.query()
    .where(inputs.userType + "Id", inputs.answeringUserId)
    .where('tagId', answerObject.tag)
    .eager('tag');

theAnswer[0].$relatedQuery('answer')
    .unrelate();

我正在尝试删除“答案”字段中的所有关系,但是数据库中似乎没有任何更改。

1 个答案:

答案 0 :(得分:0)

我在await通话中忘记了unrelate()关键字。最终的工作代码是:

await theAnswer[0].$relatedQuery('answer')
    .unrelate();