Add columns from related collection in default create action form

:warning:This is a template you must use to report issues. :warning:
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: …

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

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

The error is sended by your agent?
–>yes

How the relation have been declared inside your Sequelize model?

We have defined following in Sequelize model:


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.

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

// 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;
};
// 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;
};
// 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

let me know if that help :pray:

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


our requirement is:
We want default form for create organization should have field dept_id as shown below

I see what you want :sweat_smile: 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 ...


After that you can return to detail and finish to create your record.

Let me know if that help.

@Arnaud_Moncel Thanks a lot for your quick help.

1 Like