How to add pivot data to a relation

Feature(s) impacted

Store additional data in relations

Expected behavior

I want to see an additional field when adding a relation between two models

Context

I am on forest sequelize

Details

I am trying to add data to a pivot table of a relation. I have a table “product_packaging_material”, with columns product_id, packaging_material_id and weight. I have three questions:

1.) When I am on the relation view, is it possible to disable the “Create a new packaging material” button in the top right on the [+] button so that I can only link existing packaging materials?

2.) When on the “Add an existing packaging material” view, I only see the search input to search existing packaging materials, but I do not see the input for “weight”. From laravel I am used to say something like “belongsToMany( other )->with( pivotData )”. Is there something similar I have to add? The BelongsToManyOptions don’t seem to offer something like that.

3.) How can I filter the dropdown in the “Add existing packaging material” before showing data? The Product Model already has a “packaging_id” belongsTo relation. In the PackagingMaterial search query, I want to filter for PackagingMaterials.where( packaging_material.packaging_id = product.packaging_id )

ProductPackagingMaterial Model:

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 ProductBasePackagingMaterial = sequelize.define('productBasePackagingMaterial', {
    createdAt: {
      type: DataTypes.DATE,
      defaultValue: Sequelize.literal('now()')
    },
    updatedAt: {
      type: DataTypes.DATE
    },
    weightG: {
      type: DataTypes.DOUBLE
    }
  }, {
    tableName: 'product_base_packaging_material',
    underscored: true,
    schema: process.env.DATABASE_SCHEMA
  })

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-relationships.
  ProductBasePackagingMaterial.associate = (models) => {
    ProductBasePackagingMaterial.belongsTo(models.packagingMaterial, {
      foreignKey: {
        name: 'packagingMaterialIdKey',
        field: 'packaging_material_id'
      },
      as: 'packagingMaterial'
    })
    ProductBasePackagingMaterial.belongsTo(models.productBase, {
      foreignKey: {
        name: 'productBaseIdKey',
        field: 'product_base_id'
      },
      as: 'productBase'
    })
  }

  return ProductBasePackagingMaterial
}

Product Model:

// ...
    ProductBase.belongsToMany(models.packagingMaterial, {
      through: 'productBasePackagingMaterial',
      foreignKey: 'product_base_id',
      otherKey: 'packaging_material_id',
      as: 'packagingMaterialThroughProductBasePackagingMaterials'
    })
// ...

Thank you very much for your support!

1 Like

Hi @marksrr :wave: and welcome in our community :champagne: !

So I will try to reply to each of your question one at a time;

1) Disabling the Create a new ... inside the Has Many view:

There is only one way at the moment to disable it if you want to keep the Add an existing ... button, you will need to remove the permission to create on that collection specifically.
Would that fit your needs ? Let me know otherwise I will create a feature request for that point

2) Can I have extra fields on the Add an existing record...

At the moment, no you can’t from the many to many. The only way I see you could work around that is doing one Smart Action.
I will create a feature request to add pivot table fields when adding a record :+1:

But a solution could be to do it through the pivot table. If you have your Pivot that is declared as a relationship and you can see it on Forest, then you can use the Create a new product_packaging_material and here it will displays all your field

3) Filter data from the Add existing record...

The only way you can at the moment is to use the trick I said above, use the Create a new product_packaging_material instead.

By doing that you will have access to the Belongs To widget which allows you to filter your data based on whatever you need