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!