Hooks don't seem to work on join models

Expected behavior

I have a join model between skill and experience models. The hooks work as expected on skill and experience, but not on join itself

Actual behavior

What is the current behavior? I’ve tried all 3 methods, and nothing will trigger.

  {
    tableName: 'WorkExperiencexSkill',
    schema: process.env.DATABASE_SCHEMA,
    hooks: {
      async beforeSave(workExperiencexSkill) {
        console.log('file: work-experiencex-skill.js / line 31 / workExperiencexSkill', workExperiencexSkill);
      }
    }
  });

  WorkExperiencexSkill.addHook('afterCreate', (experience, options) => {
    console.log('file: work-experiencex-skill.js / line 38 / experience', experience);
  });

  WorkExperiencexSkill.afterCreate((experience, options) => {
    console.log('file: work-experiencex-skill.js / line 31 / options', options);
    console.log('file: work-experiencex-skill.js / line 31 / experience', experience);
  });

Context

I can add/associate an existing skill or add ones as I like:


But there afterCreate or any other hook method will never get triggered when it does.

Please provide any relevant information about your setup.

{
    "liana": "forest-express-sequelize",
    "liana_version": "7.12.2",
    "stack": {
      "database_type": "postgres",
      "engine": "nodejs",
      "engine_version": "16.9.1",
      "orm_version": "6.6.5"
    }

Hi,

What is a “join model” ?
Can you please share the models involved in the issue?

Regards

Thanks for the quick reply!

// Skill Model
const Skill = sequelize.define('Skill', {
   ...
  }, {
    tableName: 'Skill',
    schema: process.env.DATABASE_SCHEMA
  });

  Skill.associate = models => {
    Skill.hasMany(models.UserSkill, {
      as: 'SkillUserSkills',
      foreignKey: {
        field: 'SkillID',
        name: 'skillIdKey'
      },
      target: {
        field: 'ID',
        name: 'ID'
      }
    });

    Skill.belongsToMany(models.WorkExperience, {
      foreignKey: 'SkillID',
      otherKey: 'WorkExperienceID',
      through: 'WorkExperiencexSkill'
    });

  return Skill;
};

// Work Experience
  const WorkExperience = sequelize.define('WorkExperience', {
   ...
  }, {
    tableName: 'WorkExperience',
    schema: process.env.DATABASE_SCHEMA
  });

  WorkExperience.associate = models => {
    WorkExperience.belongsTo(models.User, {
      as: 'User',
      foreignKey: {
        field: 'UserID',
        name: 'userIdKey'
      },
      target: {
        field: 'ID',
        name: 'ID'
      }
    });

    WorkExperience.belongsToMany(models.Skill, {
      foreignKey: 'WorkExperienceID',
      otherKey: 'SkillID',
      through: 'WorkExperiencexSkill'
    });

  return WorkExperience;
};

The way we update this in our app is to use the afterUpdate hook on WorkExperience model, but with the way skills need to be added via Forest, we can’t go that route as it only allowed one at a time on the WorkExperiencexSkill model.

One way I thought of doing this was to create an action on the WE view, and simply save the Skills on update, but I can’t get the list of Skills to show as a multi-select in the action.

Thoughts?

Hi @David_Panzarella,

You are welcome.
Let’s focusing on the Skill and WorkExperience relationship which is a belongToMany.

You have the use-case of “adding existing or creating new” Skill onto WorkExperience and this is working. perfect.

What do you mean by “The way we update this in our app is to use afterUpdate hook […]”?
Can you please explain what is the use-case that leads you to create a hook?

(Sharing the SQL with Skill, Experience and SkillxExperience tables may also be interesting)

Regards

The issue is that there is a third join that we want to update after the association is made between WorkExperience & Skill:

Skill.hasMany(models.UserSkill, {
  as: 'SkillUserSkills',
  foreignKey: {
    field: 'SkillID',
    name: 'skillIdKey'
  },
  target: {
    field: 'ID',
    name: 'ID'
  }
});

Within the hook, I want to look at the modified association and either add or remove additional information to/from the UserSkill(SkillUserSkills) model.

Skills should also be read-only on WorkExperiencexSkill , and not allow the creation or deletion of the existing skills. We really only want to associate/disassociate and update.

Hope that explains better. Feel free to reach out to me directly if you want to have a chat about it.