# Customize relational tables

**URL:** <https://community.forestadmin.com/t/customize-relational-tables/5566>\
**Category:** Help me!\
**Created:** [November 22, 2022, 9:43am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566 "2022-11-22T09:43:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [November 22, 2022, 9:43am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/1 "2022-11-22T09:43:24Z")

</div>

## 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

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

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

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [November 22, 2022, 10:18am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/2 "2022-11-22T10:18:12Z")

</div>

Hello @Reza_Erami,

Thanks for your message 🙌

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!

---

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [November 22, 2022, 10:47am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/3 "2022-11-22T10:47:19Z")

</div>

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

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

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

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

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [November 23, 2022, 8:47am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/4 "2022-11-23T08:47:47Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 23, 2022, 9:56am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/5 "2022-11-23T09:56:08Z")

</div>

Hey @Reza_Erami 👋

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:)

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

---

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [November 29, 2022, 8:55am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/7 "2022-11-29T08:55:53Z")

</div>

@anon39848301  
yes they are both primary keys  
here is the full model

```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 GiveawayTasks = sequelize.define('giveawayTasks', {
    giveawayId: {
      type: DataTypes.UUID,
      primaryKey: true,
      allowNull: false,
    },
    taskId: {
      type: DataTypes.UUID,
      primaryKey: true,
      allowNull: false,
    },
  }, {
    tableName: 'giveaway_tasks',
    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.
  GiveawayTasks.associate = (models) => {
    GiveawayTasks.belongsTo(models.giveaway, {
      foreignKey: {
        name: 'giveawayIdKey',
        field: 'giveawayId',
      },
      as: 'giveaway',
    });
    GiveawayTasks.belongsTo(models.task, {
      foreignKey: {
        name: 'taskIdKey',
        field: 'taskId',
      },
      as: 'task',
    });
  };

  return GiveawayTasks;
};

```

---

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [November 29, 2022, 8:58am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/8 "2022-11-29T08:58:07Z")

</div>

@adriguy there is an id in database, but it’s not in the forest admin model

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 29, 2022, 10:59am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/9 "2022-11-29T10:59:06Z")

</div>

Oh, then that’s the way to go.

Updating your Sequelize model to make it the primary key should then fix the issue I guess.  
We do support composite primary key, but they are know to be the source of potential issues.

Eventually, could you try to add the id column as the primary key, and remove it from the giveawayId & taskId in Sequelize?

Thanks in advance, and let me know if that help

---

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [November 29, 2022, 12:11pm UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/10 "2022-11-29T12:11:20Z")

</div>

@anon39848301  
nope, didn’t work

so this is my model now, very basic

```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 GiveawayTasks = sequelize.define(
    'giveawayTasks',
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
        defaultValue: Sequelize.literal('uuid_generate_v4()'),
        allowNull: false,
      },
    },
    {
      tableName: 'giveaway_tasks',
      schema: process.env.DATABASE_SCHEMA,
    },
  );

  return GiveawayTasks;
};

```

and here is setting

```javascript
const { collection } = require('forest-express-sequelize');
const models = require('../models');

// This file allows you to add to your Forest UI:
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
collection('giveawayTasks', {
  actions: [],
  fields: [
    {field: "foo", get: () => "foo"}
  ],
  segments: [],
});

```

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [November 29, 2022, 2:17pm UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/11 "2022-11-29T14:17:07Z")

</div>

Mhh, weird, I can’t see any new .forestadmin-schema.json sent on your project. Did you restart your development server?

On the other end, maybe you could set the Reference field (Available in your collection settings) to replace Task and Reward always to their title (See [our documentation](https://docs.forestadmin.com/user-guide/collections/manage-your-collection-settings#general-tab)) would suit your need ?

---

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [November 30, 2022, 11:58am UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/12 "2022-11-30T11:58:18Z")

</div>

@anon39848301 yeah i was working with my personal account  
I switch to the company account and did the same changes but nothing happened

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

---

<div class="post-metadata">

**Author:** ![Reza\_Erami](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/reza_erami/32/4730_2.png) [@Reza\_Erami](https://community.forestadmin.com/u/Reza_Erami)\
**Post date:** [December 9, 2022, 3:38pm UTC](https://community.forestadmin.com/t/customize-relational-tables/5566/14 "2022-12-09T15:38:49Z")

</div>

the issue was on te both sides of the relationship  
so I had to first add id to pivot table  
then use correct methods for relationships on two ends of the pivot table  
by regenerating the models it worked.
