Many to Many relationship and though table with field

Hi team,

I have some trouble with a many to many relationship.

I have a product.
I have colors.
I have a ProductColors through table.
But this table has a isDefault field.

I can’t find a way to set this value in the UI while creating a new color for the product table UI.
(error : Field ‘isDefault’ doesn’t have a default value)
Is there a way to set this value while creating an element on the other end of the relationship ?

Thank you for your help

ps : The code

Product Colors

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/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Products_Colors = sequelize.define(
    "products_colors",
    {
      isDefault: {
        type: DataTypes.BOOLEAN,
      },
    },
    {
      tableName: "products_colors",
      timestamps: false,
    }
  );

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  Products_Colors.associate = (models) => {
    Products_Colors.belongsTo(models.product, {
      onDelete: "CASCADE",
      onUpdate: "CASCADE",
    });
    Products_Colors.belongsTo(models.color, {
      onDelete: "CASCADE",
      onUpdate: "CASCADE",
    });
  };

  return Products_Colors;
};



Colors

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/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Color = sequelize.define(
    "color",
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },

      value: {
        type: DataTypes.STRING,
        allowNull: false,
      },
    },
    {
      tableName: "color",
      timestamps: false,
    }
  );

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  Color.associate = (models) => {
    Color.belongsToMany(models.product, {
      through: "products_colors",
    });
  };

  return Color;
};

Products

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/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Product = sequelize.define(
    "product",
    {
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      },


    },
    {
      tableName: "product",
      timestamps: false,
    }
  );

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  Product.associate = (models) => {

    Product.belongsToMany(models.color, {
      through: "products_colors",
      as: "Colors",
    });


  };

  return Product;
};

`

Hi @Ilias_El-mhamdi,

Thank you for sharing your issue with us!

I’m not quite sure to understand your issue here, would you mind making a screen record of your issue (using loom for example) so I can try to reproduce it on my end?

Thank you

Hi @anon37102731,

Thank you for your quick response.

Here is the schema.
Schema

I have a through table products_colors with a field isDefault.
When I am in the ui to associate a color to a product I would to be able to set this isDefault field.
addingColor

I didn’t find how to make this filed accessible to the form. So when I try to create the color I get this error
error

I hope I was clear enough,

Thank you for your help

@Ilias_El-mhamdi, thank you for clearing this out.

For now you won’t be able to have the isDefault field on the color creation form on the related data of a product. This is because isDefault is not a field of color but one of products_colors.

As for the error triggered, I suppose that isDefault is a required field on products_colors, preventing you from creating one without this field being set. You could prevent this error by adding a default value to this field.

That being said, you should be able to display the products_colors hasmany relationship on a given product to add the relationship properly with a custom isDefault field.

I hope this will be of some help :pray:

@anon37102731 ,

okay good to know.

Thank you for your help :slight_smile: