Search code examples
node.jsknex.jsobjection.js

unrelate() doesn't seem to be changing or deleting the join table


UserTagAnswer model:

  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 model

  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'
    }
  },

I see in the database that there is a row in the join table usertaganswer_answeredoptions with all the expected information. But I want to now delete that row. I try with this:

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

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

I'm trying to erase ALL relations in the 'answer' field, but there seems to be no changes in the database.


Solution

  • I forgot the await keyword on the unrelate() call. The end working code is:

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