# Many to Many relationship and though table with field

**URL:** https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601
**Category:** Help me!
**Created:** [January 11, 2021, 2:45pm UTC](https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601 "2021-01-11T14:45:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ilias\_El-mhamdi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/ilias_el-mhamdi/32/1314_2.png) [@Ilias\_El-mhamdi](https://community.forestadmin.com/u/Ilias_El-mhamdi)
#### Post date: [January 11, 2021, 2:45pm UTC](https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601/1 "2021-01-11T14:45:50Z")

</div>

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

```javascript
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

```javascript
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

```javascript
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;
};

```

`

---

<div class="post-metadata">

### Author: ![anon37102731](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@anon37102731](https://community.forestadmin.com/u/anon37102731)
#### Post date: [January 11, 2021, 3:00pm UTC](https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601/2 "2021-01-11T15:00:27Z")

</div>

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](https://www.loom.com/) for example) so I can try to reproduce it on my end?

Thank you

---

<div class="post-metadata">

### Author: ![Ilias\_El-mhamdi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/ilias_el-mhamdi/32/1314_2.png) [@Ilias\_El-mhamdi](https://community.forestadmin.com/u/Ilias_El-mhamdi)
#### Post date: [January 11, 2021, 3:21pm UTC](https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601/3 "2021-01-11T15:21:04Z")

</div>

Hi @anon37102731,

Thank you for your quick response.

Here is the schema.  
 ![Schema](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/b4755b336780876d9bd219427a828b6a49a3bebb.png)

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](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/4c86fdab3f7e1b608d88ef0e06eb0f488e9e16e9.jpeg)

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](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9c8ea44749065cd2cdae0fc9eb3c7a336fc9c0c7.png)

I hope I was clear enough,

Thank you for your help

---

<div class="post-metadata">

### Author: ![anon37102731](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@anon37102731](https://community.forestadmin.com/u/anon37102731)
#### Post date: [January 11, 2021, 4:23pm UTC](https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601/4 "2021-01-11T16:23:04Z")

</div>

@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 🙏

---

<div class="post-metadata">

### Author: ![Ilias\_El-mhamdi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/ilias_el-mhamdi/32/1314_2.png) [@Ilias\_El-mhamdi](https://community.forestadmin.com/u/Ilias_El-mhamdi)
#### Post date: [January 11, 2021, 4:26pm UTC](https://community.forestadmin.com/t/many-to-many-relationship-and-though-table-with-field/1601/5 "2021-01-11T16:26:04Z")

</div>

@anon37102731 ,

okay good to know.

Thank you for your help 🙂
