Creating many to many relation when using custom property

If there is no custom property, forestadmin detects many to many relationships. How should we manage this relationship when we use a custom property?

CREATE TABLE exercise_instruction (
    exercise_id INT NOT NULL,
    instruction_id INT NOT NULL,
    rank INT NOT NULL DEFAULT 0,
    CONSTRAINT fk_exercise FOREIGN KEY(exercise_id) REFERENCES exercises(id),
    CONSTRAINT fk_instruction FOREIGN KEY(instruction_id) REFERENCES instructions(id)
);
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models
module.exports = (sequelize, DataTypes) => {
  const { Sequelize } = sequelize;
  // This section contains the fields of your model, mapped to your table's columns.
  // Learn more here: https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const ExerciseInstruction = sequelize.define('exerciseInstruction', {
    exerciseId: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
    },
    instructionId: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
    },
    rank: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      allowNull: false,
    },
  }, {
    tableName: 'exercise_instruction',
    underscored: true,
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-relationships.
  ExerciseInstruction.associate = (models) => {
    ExerciseInstruction.belongsTo(models.exercises, {
      foreignKey: {
        name: 'exerciseIdKey',
        field: 'exercise_id',
      },
      as: 'exercise',
    });
    ExerciseInstruction.belongsTo(models.instructions, {
      foreignKey: {
        name: 'instructionIdKey',
        field: 'instruction_id',
      },
      as: 'instruction',
    });
  };

  return ExerciseInstruction;
};

Hello @Murat_AKSU !

Can you share the required relevant information about your configuration (it helps us a lot in giving the right answer)?

  • Project name: …
  • Team name: …
  • Environment name: …
  • Agent (forest package) name & version: …
  • Database type: …
  • Recent changes made on your end if any: …

I’m not quite sure what is the intended behavior your expecting. Can you describe it?

As a side note, here is the relevant documentation about Sequelize Many-to-Many association.

Kind regards,
Morgan