Customize relational tables

Feature(s) impacted

relationships and collections

Observed behavior

smart field doesn’t fetch the record

Expected behavior

get the record in smart field in a relational table

Context

we have two tables that have many-to-many relationship
let’s call them tasks, and rewards
both rewards, and tasks has id, and title fields
to connect tasks and rewards we have a pivot table called rewards_tasks which has a reward_id, and task_id

in the rewards_tasks collection I’m trying to add a smart field to show the title of rewards, and the title the of tasks instead of their id, but when I try to add a smart field, it doesn’t show the value, it doesn’t even try to fetch the record to get the title from

this code works on other collections that are not relational, but it’s not working on this one
is there any other way to get the title from the related entities?

thank you

  • Project name: …Merit Circle
  • Team name: Merit Circle
  • Environment name: all

Hello @Reza_Erami,

Thanks for your message :raised_hands:

There should be no issue with what you’re trying to achieve.
I just tried on my side and I didn’t face any issue.

Are you able to see the Task Title field in the UI?
When it is displayed, can you see the logs in your server?
The record you are displaying (a reward_task) should be able to access to a reward (or at least a reward_id, so that you can fetch the reward in that case) and so access to the reward title.

Let me know where you’re at in your test.
Cheers!

nope, there is no log available, seems “get” method doesn’t work in join tables
for example, this code works in the rewards (giveaways) table
image

and as you see the same code doesn’t work on a join table

Hello @Reza_Erami,

We have an idea of what could be the issue.
Do you have an id field on this join table (rewards_tasks)? Or does it use a composite key?

Let me know!

1 Like

Hey @Reza_Erami :wave:

I just did a quick test on my end and I’m currently unable to reproduce the issue.

Could you confirm that your join table indeed consider task_id & reward_id as a composite primary key ?

(Like in the following Sequelize example:)

// 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 UserProject = sequelize.define('userProject', {
    user_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
    project_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
  }, {
    tableName: 'user_project',
    underscored: true,
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });
  UserProject.removeAttribute('id');
  // This section contains the relationships for this model. See: https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-relationships.
  UserProject.associate = (models) => {
    UserProject.belongsTo(models.project, {
      foreignKey: {
        name: 'projectIdKey',
        field: 'project_id',
      },
      as: 'project',
    });
    UserProject.belongsTo(models.user, {
      foreignKey: {
        name: 'userIdKey',
        field: 'user_id',
      },
      as: 'user',
    });
  };

  return UserProject;
};

Thanks in advance :pray: