# Add columns from related collection in default create action form

**URL:** https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011
**Category:** Help me!
**Created:** [July 25, 2022, 8:29am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011 "2022-07-25T08:29:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Sudarshana\_Patil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sudarshana_patil/32/4007_2.png) [@Sudarshana\_Patil](https://community.forestadmin.com/u/Sudarshana_Patil)
#### Post date: [July 25, 2022, 8:29am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/1 "2022-07-25T08:29:56Z")

</div>

> ⚠ **This is a template you must use to report issues**. ⚠  
> You can also drag images, videos and include `Preformatted text`.

## Feature(s) impacted

Not able to create data due mandatory fields from related table not available in default create form.

## Observed behavior

Getting error while creating data as mandatory columns are missing.

## Expected behavior

should be able to create data with all mandatory columns

## Failure Logs

> In this _optional section_, please:
> 
> - include any relevant log snippets if necessary,
> - or remove this section if left empty.

## Context

> Please provide in this **mandatory section** , the relevant information about your configuration:

- Project name: Curator
- Team name: Developer
- Environment name: ALL (Staging, Development, Production)
- Agent type & version: \* Package Version: 8.3 (forest-express-sequelize)

- Express Version: 4.17.1
- Sequelize Version: 6.6.5
- Database Dialect: MySql
- Database Version: 5.7.24

- Recent changes made on your end if any: …

---

<div class="post-metadata">

### Author: ![Arnaud\_Moncel](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud_moncel/32/24_2.png) [@Arnaud\_Moncel](https://community.forestadmin.com/u/Arnaud_Moncel)
#### Post date: [July 25, 2022, 9:39am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/2 "2022-07-25T09:39:36Z")

</div>

Hi @Sudarshana_Patil 👋 Can you send us more details please?  
Maybe screenShot?  
Explanation of your table architectures?  
The error is sended by your agent?  
🙏

---

<div class="post-metadata">

### Author: ![Sudarshana\_Patil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sudarshana_patil/32/4007_2.png) [@Sudarshana\_Patil](https://community.forestadmin.com/u/Sudarshana_Patil)
#### Post date: [July 25, 2022, 1:53pm UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/3 "2022-07-25T13:53:56Z")

</div>

Hi @Arnaud_Moncel below are the details:

Explanation of table architectures:  
So we have two tables

- Organization - org\_id(PK), org\_name
- Department - dept\_id(PK), dept\_name  
and this two tables have M:M relationship via table Organization\_Department\_Map(org\_id(FK),dept\_id(FK))  
our use case is:  
while creating data for organization dept\_id is mandatory. And for default create action form dept\_id is not available so its throwing error  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/5/51c4359da2f0f6f06be968fde7569a9639ce46c2.png)

The error is sended by your agent?  
–\>yes

---

<div class="post-metadata">

### Author: ![Arnaud\_Moncel](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud_moncel/32/24_2.png) [@Arnaud\_Moncel](https://community.forestadmin.com/u/Arnaud_Moncel)
#### Post date: [July 25, 2022, 2:07pm UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/4 "2022-07-25T14:07:05Z")

</div>

How the relation have been declared inside your Sequelize model?

---

<div class="post-metadata">

### Author: ![Sudarshana\_Patil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sudarshana_patil/32/4007_2.png) [@Sudarshana\_Patil](https://community.forestadmin.com/u/Sudarshana_Patil)
#### Post date: [July 26, 2022, 6:39am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/5 "2022-07-26T06:39:48Z")

</div>

We have defined following in Sequelize model:

```javascript

Organization_Department_Map.associate = (models) => {
      models.Organization.belongsToMany(models.Department, {
        through: 'Organization_Department_Map',
        foreignKey: 'org_id',
        otherKey: 'dept_id',
      });
      models.Department.belongsToMany(models.Organization, {
        through: 'Organization_Department_Map',
        foreignKey: 'dept_id',
        otherKey: 'org_id'
      });
    };

```

our use case is:  
while creating data for organization table dept\_id is mandatory which we are checking inside hook . And for default create action form dept\_id field is not available, so its throwing error.

---

<div class="post-metadata">

### Author: ![Arnaud\_Moncel](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud_moncel/32/24_2.png) [@Arnaud\_Moncel](https://community.forestadmin.com/u/Arnaud_Moncel)
#### Post date: [July 26, 2022, 7:23am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/6 "2022-07-26T07:23:25Z")

</div>

I did a test on your architecture. And with theses three models it work fine IMO.

```javascript
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/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/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Org = sequelize.define('org', {
    name: {
      type: DataTypes.STRING,
    },
  }, {
    tableName: 'org',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  Org.associate = (models) => {
    Org.belongsToMany(models.dep, {
      through: 'orgDep',
      foreignKey: 'orgId',
      otherKey: 'depId',
      as: 'depThroughOrgDeps',
    });
  };

  return Org;
};

```

```javascript
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/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/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const Dep = sequelize.define('dep', {
    name: {
      type: DataTypes.STRING,
    },
  }, {
    tableName: 'dep',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  Dep.associate = (models) => {
    Dep.belongsToMany(models.org, {
      through: 'orgDep',
      foreignKey: 'depId',
      otherKey: 'orgId',
      as: 'orgThroughOrgDeps',
    });
  };

  return Dep;
};

```

```javascript
// This model was generated by Forest CLI. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/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/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
  const OrgDep = sequelize.define('orgDep', {}, {
    tableName: 'orgDep',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  OrgDep.associate = (models) => {
    OrgDep.belongsTo(models.dep, {
      foreignKey: {
        name: 'depIdKey',
        field: 'depId',
      },
      as: 'dep',
    });
    OrgDep.belongsTo(models.org, {
      foreignKey: {
        name: 'orgIdKey',
        field: 'orgId',
      },
      as: 'org',
    });
  };

  return OrgDep;
};

```

After that you can go to your layout editor and hide all id fields as below  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/9def265a105bc9a8b6c1fa41af88aa9b9420faae.png)

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/8410bbc39a55aafe23d4807ba52570ff423bdde4.png)

let me know if that help 🙏

---

<div class="post-metadata">

### Author: ![Sudarshana\_Patil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sudarshana_patil/32/4007_2.png) [@Sudarshana\_Patil](https://community.forestadmin.com/u/Sudarshana_Patil)
#### Post date: [July 26, 2022, 9:52am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/7 "2022-07-26T09:52:25Z")

</div>

Our use case is to create Orgs with following columns with default create action  
1.OrgId  
2.OrgName  
3.DepId

 ![Default-create-org](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/b9aab88d69e085b1a4ae0ec41bfab5489c2ebfe6.png)  
our requirement is:  
We want default form for create **organization** should have field dept\_id as shown below  
 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/5/51c4359da2f0f6f06be968fde7569a9639ce46c2.png)

---

<div class="post-metadata">

### Author: ![Arnaud\_Moncel](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud_moncel/32/24_2.png) [@Arnaud\_Moncel](https://community.forestadmin.com/u/Arnaud_Moncel)
#### Post date: [July 26, 2022, 10:19am UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/9 "2022-07-26T10:19:01Z")

</div>

I see what you want 😅 unfortunately it’s not working like that.  
To do what you want you can go to the `related data tab` and click on the `plus button` and click on the `add an existing ...`

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/4a3612639c719df30e33b5556d295670d689c1f8.png)  
After that you can return to detail and finish to create your record.

Let me know if that help.

---

<div class="post-metadata">

### Author: ![Sudarshana\_Patil](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/sudarshana_patil/32/4007_2.png) [@Sudarshana\_Patil](https://community.forestadmin.com/u/Sudarshana_Patil)
#### Post date: [July 28, 2022, 3:13pm UTC](https://community.forestadmin.com/t/add-columns-from-related-collection-in-default-create-action-form/5011/10 "2022-07-28T15:13:18Z")

</div>

@Arnaud_Moncel Thanks a lot for your quick help.
